开发者

How can I divide unity configuration section into several files?

开发者 https://www.devze.com 2023-03-14 13:47 出处:网络
In my project there is some common package which gets its dependencies resolved by the UnityContainer which is defined in unity.config file.

In my project there is some common package which gets its dependencies resolved by the UnityContainer which is defined in unity.config file. There is another custom package which I want to have its own custom UnityContainer in a seperated CustomUn开发者_JAVA百科ity.config file.

In runtime I want both files to be loaded and when I get the unity section, I want it to contain both UnityContainers.

How can i achieve it?

Thanks!


The UnityContainer.LoadConfiguration method can be called multiple times on the same container. Each time it loads whatever's in that configuration section, but it doesn't remove what was previously in the container - it's additive. If there's a conflict (both sections configure the same type) then last one in wins.

So, the approach would be to use the ConfigurationManager APIs to load your two separate UnityConfigurationSections, and then call LoadConfiguration twice, once for each configuration section. That should be all you have to do.


I wrote a library that lets you write your ioc container configuration in modules. It supports unity but you will have to configure your container in codes instead of using the files. I don't know if will solve your problem, but you can check it out at bootstrapper.codeplex.com

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号