http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx
UnityContainer and UnityConfigurationSection - Populating Unity Container via App.config or Web.config
The Unity IoC Tutorials provided to date using the ASP.NET MVC Framework and ASP.NET Model-View-Presenter have programmatically added services and components via the Register and SetSingleton Methods on UnityContainer. In this Unity Tutorial, I will populate the UnityContainer from services registered in a Web.config file. One can easily use an App.config file as well.
Unity Tutorials
For reference, here are links to some Unity Tutorials that discuss using Unity with the ASP.NET MVC Framework and ASP.NET Model-View-Presenter:
- Unity IoC and ASP.NET MVC Framework - Dependency Injection of Controllers
- Unity IoC Dependency Injection and ASP.NET Model-View-Presenter
Sample Unity Code
In this sample code, I will be using the ASP.NET Model-View-Presenter Tutorial as a basis for the code. Essentially, I will be modifying the code just a bit in 2 ways:
- Adding the Unity Configuration Information in Web.config
- Changing the InitializeContainer Method in Global.asax.cs to read and populate the UnityContainer from the UnityConfigurationSection in Web.config
UnityConfigurationSection in Web.config
In the previous tutorial using the Unity Dependency Injection Tool with ASP.NET Model-View-Presenter, I populated the UnityContainer in an InitializeContainer Method in the Global.asax.cs programmatically using the Register and SetSingleton Methods on UnityContainer:
private static void InitializeContainer()
{
if (_container == null)
_container = new UnityContainer();
_container
.RegisterType<IBlogDataSource, BlogDataSource>
(new ContainerControlledLifetimeManager())
.RegisterType<ILogger, NullLogger>()
(new ContainerControlledLifetimeManager());
}
Rather than adding the components programmatically, I will instead create the appropriate configuration sections in my Web.config to register the services:
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<containers>
<container>
<types>
<type
type="WebApplication1.ILogger,WebApplication1"
mapTo="WebApplication1.NullLogger,WebApplication1"
lifetime="Singleton" />
<type
type="WebApplication1.IBlogDataSource,WebApplication1"
mapTo="WebApplication1.BlogDataSource,WebApplication1"
lifetime="Singleton" />
</types>
</container>
</containers>
</unity>
You can learn more about these configuration settings in the Unity Documentation, but in general, I am just mapping my interfaces to concrete types and setting the lifetime as Singletons.
Populating the UnityContainer via App.config or Web.config
Once the configuration settings are completed in the App.config or Web.config file, I can change my InitializeContainer Method to now populate the container based on the services registered in the configuration file. The new InitializeContainer Method becomes:
private static void InitializeContainer()
{
if (_container == null)
_container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)
ConfigurationManager.GetSection("unity");
section.Containers.Default.GetConfigCommand().Configure(_container);
}
Again, pretty self-explanatory. We read the UnityConfigurationSection from the default configuration file, Web.config, and then populate the UnityContainer as show above.
Conclusion
Unity can read its configuration information from a configuration file, such as App.Config or Web.config. Even though I focused on the ASP.NET Webforms Tutorial using Model-View-Presenter, as you would expect it will also work with ASP.NET MVC Framework, Winform, WPF, etc.
I hope this helps.
David Hayden
&&
Unity系列文章
http://www.cnblogs.com/shanyou/category/126800.html

浙公网安备 33010602011771号