In March 12, unity released a version earlier than the official release. This version provides the installer and a configuration API for dependency injection implementation: setter injection. In the previous version, [dependency] is required for property injection. This design unity will intrude into your components. You can use configuringinjection.
For example, stoplightpresenter depends on stoplight and stoplightschedule. You can tag [dependency] In the attribute or remove this tag, and then configure the API in unitycontainer by configuring the following:
Public class stoplightpresenter
{
Private stoplight;
Private stoplightschedule schedule;
// [Dependency]
Public stoplight
{
Get {return stoplight ;}
Set {stoplight = value ;}
}
// [Dependency]
Public stoplightschedule schedule
{
Get {return schedule ;}
Set {Schedule = value ;}
}
Private istoplightview view;
......
}
The configuration code is as follows:
Iunitycontainer Container = new unitycontainer ()
. Addnewextension <simpleeventbrokerextension> ()
. Registertype <ilogger, tracelogger> ()
. Registertype <istoplighttimer, realtimetimer> ();
Container. Configure <injectedmembers> ()
. Configureinjectionfor <stoplight. Logic. stoplight> (
New injectionproperty ("logger ")
);
Container. Configure <injectedmembers> ()
. Configureinjectionfor <stoplightpresenter> (
New injectionproperty ("stoplight "),
New injectionproperty ("Schedule ")
);
Although the development of the Unity Application Block is not long, it is mainly based on the development of objectbuilder that emerged from enterprise class library 2.0. The next Microsoft Enterprise Library version V4-injection of Preset support dependency. Dependency injection is provided by containers independently or as part of the database. You need to learn more about the unity Application Block. You can refer to articles about terrylee, doriandeng, and overred in the garden:
Terrylee's unity Application Block
Dependency injection container unity Application Block (1): Quick Start to dependency injection container (Unity) preview in Enterprise Library 4.0
Doriandeng unity
Use Unity (1): Introduction to unity Application Block containers
Use Unity (2): Configure unity, read configuration information, and obtain objects
Use unity Application Block (3): understand and use the dependency injection key
Unity Feb 26 weekly drop
Overred ① Net Framework
Dependency injection container unity Application Block (2): Spring of Unity
By the way, you can also learn English:
Unity dependency injection IOC screencast
Unity IOC and ASP. net mvc Framework-dependency injection of controllers
Unity nested containers-iunityparentcontainer and createchildcontainer
Unity IOC-February 26 weekly drop-lifetimemanagers teardown extensions and idisposable
Using unity and the ASP. net mvc preview 2