如何管理 .NET 中多个类库的配置?
我正在寻找管理具有多个类库的项目配置的最佳实践。我正在寻找可维护性和易于实施性。
让我们假设一个简单的例子:一个带有 2 个类库的控制台项目。每个类库都需要自己的配置设置,并且有一些设置是多个类库通用的。
类库 1
- CL1Setting
- GlobalSetting
类库 2
- CL2Setting
- GlobalSetting
第一种方法是在主项目上创建所有必要的设置:
- GlobalSetting
- CL1Setting
- CL2Setting
但这会带来几个问题:
- 如果有很多设置,它可能会很快变得混乱。
- 维护起来并不容易:如何知道每个库需要哪些设置?
- 它可能会造成命名冲突。如果 CL1Setting 和 CL2Setting 有相同的名称怎么办?
对我来说理想的解决方案(尽管恐怕不可能)是在单独的文件或至少不同的部分中具有自定义库设置。像这样:
<configuration>
<appSettings>
<add key="globalSetting" value="cl1Global"/>
</appSettings>
<appSettings file="CL1.config" >
<add key="cl1setting" value="cl1setting1"/>
</appSettings>
<appSettings file="CL2.config">
<add key="cl2setting" value="cl2setting2"/>
</appSettings>
</configuration>
有什么建议吗?
编辑
正如 Ken Henderson 所建议的,配置部分是另一种方法。然而,虽然它们有自己的优点,但它们需要编码,所以我觉得它不太理想。 (尽管这最终可能是最好的选择)
编辑 2
joseph.ferris 建议查看 CodePlex (csd.codeplex.com) 上的配置部分设计器,这很好。我发现了更多问题,请在此处报告(如果有人感兴趣)http://csd.codeplex.com/discussions /278354
I'm looking for a best practice for managing configuration on a project with multiple class libraries. I'm looking for maintainability and ease of implementation.
Let's assume a simple example: A console project with 2 class libraries. Each class library need their own configuration settings, and there are some settings that are common to several.
Class Library 1
- CL1Setting
- GlobalSetting
Class Library 2
- CL2Setting
- GlobalSetting
A first approach would be to create all the necessary settings on the main project:
- GlobalSetting
- CL1Setting
- CL2Setting
But this present several problems:
- It can get cluttered fast if there are lots of settings.
- It is not easy to maintain: How to know which settings are needed for each library?
- It can create naming conflicts. What if CL1Setting and CL2Setting would have the same name?
An ideal solution for me (although I'm afraid not possible) would be having custom library settings in separate files, or at least different sections. Something like this:
<configuration>
<appSettings>
<add key="globalSetting" value="cl1Global"/>
</appSettings>
<appSettings file="CL1.config" >
<add key="cl1setting" value="cl1setting1"/>
</appSettings>
<appSettings file="CL2.config">
<add key="cl2setting" value="cl2setting2"/>
</appSettings>
</configuration>
Any suggestions?
EDIT
As Ken Henderson suggests, config sections are another approach. However, although with their own advantages, they require coding, so I don't find it ideal though. (This will probably end up being the best option though)
EDIT 2
joseph.ferris suggestion to look at Configuration Section Designer on CodePlex (csd.codeplex.com) was good. I found further problems, reported here (in case some is interested) http://csd.codeplex.com/discussions/278354
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找的是自定义 配置部分 而不是 appSettings 。这通常由第三方库(首先想到的是 log4net)使用,以提供一种通过应用程序/Web 配置文件配置其设置的方法。请注意,这也为 MS 如何创建其配置部分提供了基础。
我已经在几个不同的项目中成功地使用了它,其中一个项目能够向分析程序添加新的算法实现。
I think what you are looking for is a custom configuration section instead of appSettings. This is commonly used by 3rd party libraries (log4net is the first that comes to mind) to provide a way to configure their settings via your app/web config file. Note that this also provides the basis for how MS creates their configuration sections.
I've successfully used this in several different projects including one that included the ability to add new implementations of algorithms to an analysis program.
您可以使用自己的命名约定来降低 appSettings 命名冲突的风险。和/或创建自定义配置部分。
我不确定管理员是否需要知道哪个设置属于哪个库,但命名约定有助于促进逻辑分组 - 我会使用对管理员有意义的前缀,而不是说类库名称 - 例如“日志记录” ”。用于与日志记录相关的应用程序设置。
You could use your own naming convention to reduce the risk of appSettings naming collisions. And/or create custom configuration sections.
I'm not sure an administrator would need to know which setting belongs to which library, but a naming convention helps promote a logical grouping - I would use prefixes that are meaningful to an administrator, rather than say a class library name - e.g. "Logging." for appSettings related to logging.
Microsoft 在某些代码中使用冒号作为命名空间的分隔符。
示例 Azure样本。这里他们使用
ida:
作为前缀。在 Microsoft ASP.NET 的 Web.config 文件中可以看到:
Microsoft uses colon as a delimiter for namespace in some of their code.
Example their Azure samples. Here they use
ida:
as prefix.In Microsoft ASP.NET in the Web.config file you can see: