注入应用程序配置的最佳方式
好吧,我正在进军这个奇妙的网站,带着一个关于将配置设置注入应用程序组件的正确方法的问题。所以,概述是:我有一个用 C# .Net 3.5 编写的应用程序。它由 3 个组件组成 - 核心、数据和服务。数据&服务程序集需要从 app.config 检索设置,这是通过设置文件完成的,例如
代码:
public static String RequestQueueConnectionString
{
get { return ConnectionSettings.Default.RequestQueueConnectionString; }
}
配置:
<applicationSettings>
<MyNamespace.Data.ConnectionSettings>
<setting name="RequestQueueConnectionString" serializeAs="String">
...
现在,程序集都是使用 IoC 的 StructureMap 设置的 - 在我看来,这应该提供我正在寻找的答案,但我只是看不到它!
IoC:
public static void ConfigureStructureMap(IContainer container)
{
container.Configure(x => ...
...
我想要做的是将已填充到 IoC 容器中的配置类注入,以便这些设置用于该程序集,而不是设置文件/app.config 中指定的设置。所以也许:
public static void ConfigureStructureMap(IContainer container, MyConfigClass config)
{
container.Configure(x => x.For<DataConfig>()
.Singleton()
.Use ???
...
我希望我在这里提供了足够的细节 - 如果我没有提供足够的细节,请原谅新手,请让我知道还有什么对回答这个问题有帮助!
Well, I'm making my foray into this fantastic site with a question about the correct way to inject configuration settings into application components. So, the overview is : I have an application written in C# .Net 3.5. It consists of 3 assemblies - a Core, a Data and a Service. The data & service assemblies require settings retrieved from the app.config, which is done via a settings file, eg.
Code :
public static String RequestQueueConnectionString
{
get { return ConnectionSettings.Default.RequestQueueConnectionString; }
}
Config :
<applicationSettings>
<MyNamespace.Data.ConnectionSettings>
<setting name="RequestQueueConnectionString" serializeAs="String">
...
Now, the assemblies are all setup using StructureMap for IoC - which to my mind should provide the answer to what I am looking for, but I just can't quite see it!
IoC :
public static void ConfigureStructureMap(IContainer container)
{
container.Configure(x => ...
...
What I want to be able to do is to inject a configuration class already populated into the IoC container such that those settings are used for that assembly, NOT those specified in the settings file / app.config. So perhaps :
public static void ConfigureStructureMap(IContainer container, MyConfigClass config)
{
container.Configure(x => x.For<DataConfig>()
.Singleton()
.Use ???
...
I hope I have provided enough details here - forgive a newbie if I have not and please let me know what else would be helpful in answering this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,经过大量搜索和反复试验后,我收到了 @default.kramer 的链接,我正确地遵循了该链接!再次经过一些尝试和错误(我认为最好的方法),我设法得到了我正在寻找的解决方案。现在,虽然您可以点击链接(我强烈建议这样做),但我将在实施时发布我的问题的解决方案。希望这可以帮助有类似问题的人。
所以,我现在有了这样的配置设置类:
现在,在我使用设置文件从 app.config 文件中检索配置之前。这显然有利于确保我可以灵活地更改配置设置,但它给我带来了无法轻松测试这些设置的问题。假设 9/10 测试要求服务处于活动状态,但 1 测试想要测试“ServiceIsActive = false;”,现在我遇到了麻烦。
不过,现在我可以从测试中注入配置:
然后在现实世界中,我可以从 app.config 获取设置:
然后实际的配置类如下所示:
为了完整性,界面看起来如下像这样:
最后,配置文件如下所示:
现在,任何目光敏锐的人都会发现“MyNamespace.XmlConfigurator”。我在一次 Google 旅程中发现了这一点,该代码允许您将 Xml 配置反序列化为您想要的类(如本示例所示)。因此,为了确保您拥有使该技术发挥作用的完整代码,下面是 XmlConfigurator 的代码。我不记得我在哪里看到它,但非常感谢写它的人!
现在你就拥有了!我希望这能让任何有类似问题的人都能解决它,并且足够清楚,可以遵循!
So, after a lot of searching and trial and error, I was presented with @default.kramer's link, which I duely followed! With a little bit of trial and error, again (best way in my opinion), I managed to get the solution I was looking for. Now, whilst you can follow the link (and I would highly suggest doing so), I am going to post the solution to my question as I implemented it. Hopefully this might help someone with a similar problem.
So, I now have my configuration setup class like so :
Now, before I was using a settings file to retrieve the configuration out of the app.config file. This was obviously good for ensuring I had flexibility in changing my config settings, but it left me with the problem of not being able to easily test those settings. Say 9/10 tests required the service to be active, but 1 test wanted to test "ServiceIsActive = false;", now I'm in trouble.
Now, however, I am able to inject the configuration from the test :
And then in the real world I am able to get the settings from app.config :
And then the actual config class looks like :
For the sake of completeness the interface looks like so :
And finally, the config file looks like this :
Now, anyone with a keen eye will spot "MyNamespace.XmlConfigurator". I found this on one of my Google journeys, and the code allows you to deserialize an Xml config into a class of your desire (as shown in this example). So, to ensure you have the complete code to make this technique work, below is the code for the XmlConfigurator. I cannot remember where I came across it, but a big thanks to the person who wrote it!!
And there you have it! I hope this allows anyone with a similiar issue to resolve it and is clear enough to follow!