5 .net configuration dependency-injection castle-windsor
我有很多城堡xml配置,其中像connectionstring这样的属性也保存在.Net配置部分下.
我希望能够使用容器读出属性,但无法看到执行此操作的机制.
即
<castle>
<configuration>
<properties>
<connectionString>Data Source=MyServer;Initial Catalog=YadaYada;User ID=me;Password=IAmGod</connectionString>
</properties>
<components>
Run Code Online (Sandbox Code Playgroud)
...
显然属性存在,因为它们存在组件依赖性.我可以解析组件而不是属性.
当然我可以新建一个组件只是为了读出注入它的属性城堡,或者我可以将所有属性包装在专门用于保存名称/值对的组件中.但我认为有一个简单的container.resolve("property.connectionstring")表示法.
*编辑不是很好用markdown,上面的意思是xml部分.
你是不是在思考这个问题?
当然,使用连接字符串的对象应该在构造函数中具有 ConnectionString 属性,并且将依赖项推入其中,并在配置的组件部分中使用如下内容:
<component type="SessionManager">
<parameters>
<connectionString>#{connectionString}</connectionString>
</parameters>
</component>
Run Code Online (Sandbox Code Playgroud)
这样,连接字符串将自动传入,并且您的组件根本不需要了解容器 - 解耦和内聚!