如何管理每个开发人员的配置设置

时间:2020-03-05 18:41:23  来源:igfitidea点击:

在.NET项目中,假设我们有一个配置设置,例如存储在app.config文件中的连接字符串,这对于我们团队中的每个开发人员都是不同的(他们可能使用本地SQL Server或者特定的服务器实例,或者使用远程服务器等)。

如何构造解决方案,以使每个开发人员都可以拥有自己的开发"首选项"(即未检入源代码管理),但是提供了检入源代码控制中的默认连接字符串(从而为构建过程提供正确的默认值或者新开发者)。

编辑:@Jonathon建议的"文件"方法可以和connectionStrings`部分一起使用吗?

解决方案

回答

我总是为我的配置文件制作模板。

例如,我使用NAnt构建项目。我签入了一个名为local.properties.xml.template的文件。如果local.properties.xml不存在,我的NAnt构建将警告开发人员。该文件中将包含工作站特定的设置。该模板将被检入源代码管理,但实际配置不会。

回答

我使用的是过时的设计,可以正常工作。

  • /_Test__app.config
  • /_Prod__app.config
  • /app.config

然后在我的nant脚本中,我有一个任务可以复制当前的构建环境以及_ app.config,并将其复制到app.config。

它很讨厌,但是我们不能说提供商通过查看" dev"或者" prod"连接字符串而只有3个命名的连接字符串,因此无法在提供商和ConfigurationManager之间进行欺骗。

南特任务:

<target name="copyconfigs" depends="clean">
  <foreach item="File" property="filename" unless="${string::get-length(ConfigPrefix) == 0}">
   <in>
     <items>
       <include name="**/${ConfigPrefix}App.config" />
       <include name="**/${ConfigPrefix}connectionstrings.config" />
       <include name="**/${ConfigPrefix}web.config" />
     </items>
   </in>
   <do>
    <copy overwrite="true" file="${filename}" tofile="${string::replace(filename, ConfigPrefix,'')}" />
   </do>
  </foreach></target>

回答

AppSettings可以被本地文件覆盖:

<appSettings file="localoveride.config"/>

这样,每个开发人员都可以保留自己的本地设置。

至于连接字符串,在理想情况下,所有开发人员都应连接到测试数据库,而不是每个都运行SQL Server。

但是,我发现最好在源代码管理中保留一个名为Web.Config.Prd的文件,并将其用于构建部署。如果有人修改了web.config,他们还必须将更改添加到.PRD文件中...那里没有很好的自动化方法:(

回答

Can the "file" method suggested by @Jonathon be somehow used with the connectionStrings section?

不,但是没有什么阻止我们将ConnectionString存储为AppSettings项。

回答

Edit: Can the "file" method suggested
  by @Jonathon be somehow used with the
  connectionStrings section?

或者,我们可以在签入的配置文件中包含多个连接字符串,然后使用AppSettings键确定要使用哪个ConnectionString。为此,我在代码库中有以下内容:

public class ConnectionString
{
    public static string Default
    {
        get 
        { 
            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultConnectionStringName"]))
                throw new ApplicationException("DefaultConnectionStringName must be set in the appSettings");

            return GetByName(ConfigurationManager.AppSettings["DefaultConnectionStringName"]);
        }
    }

    public static string GetByName(string dsn)
    {
        return ConfigurationManager.ConnectionStrings[dsn].ConnectionString;
    }
}