简体   繁体   中英

How can I divide unity configuration section into several files?

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 CustomUnity.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM