Transferred from: http://blog.csdn.net/wanzhuan2010/article/details/7763280
Unity is a lightweight, extensible, dependency injection container implemented by the Microsoft patterns& Practices group in C # that facilitates the development of loosely coupled applications for developers.
Has the following advantages:
1. Simplifies the creation of objects, especially for hierarchical object structures and dependencies;
2. Abstraction of requirements, allowing developers to specify dependencies in runtime or configuration files, simplifying management of crosscutting concerns;
3. Delays in configuring components for containers adds flexibility;
4. Service positioning capability, which enables customers to store or cache containers;
5. Instance and type interception
: http://unity.codeplex.com/
The latest version is the Unity 3.0 for. NET 3.5 Preview
The following program is in version 2.1
Start the Unity tour below
(a) My first unity Demo
Create a new console application that references the Microsoft.Practices.Unity.dll file;
Create a new bird interface that defines a method of bird barking;
[CSharp]View Plaincopy
- <summary>
- Bird interface
- </summary>
- Public Interface Ibird
- {
- // <summary>
- /// speech
- // </summary>
- void Say ();
- }
To implement this interface:
[CSharp]View Plaincopy
- <summary>
- Swallow
- </summary>
- Public class Swallow:ibird
- {
- public void Say ()
- {
- Console.WriteLine ("The Swallow is barking ...");
- }
- }
Implement IOC inversion control through unity in the Mian method;
[CSharp]View Plaincopy
- static void Main ( string[] args)
- {
- //instantiation of a controller
- Iunitycontainer UnityContainer = new UnityContainer ();
- //Implementation injection
- Unitycontainer.registertype<ibird, swallow> ();
- Ibird bird = unitycontainer.resolve<ibird> ();
- Bird. Say ();
- Console.read ();
Operation Result:
This small instance has implemented a simple IOC inversion of control.
What if there are two implementations of an interface? Is it possible to add a code similar to the one below? Try it below.
Unitycontainer.registertype<ibird, swallow> (); We add a sparrow class to the original program to implement the Ibird interface:
[CSharp]View Plaincopy
- Public class Sparrow:ibird
- {
- public void Say ()
- {
- Console.WriteLine ("Sparrow is calling ....");
- }
- }
Main Method Code:
[CSharp]View Plaincopy
- Instantiate a controller
- Iunitycontainer UnityContainer = new UnityContainer ();
- Implementation injection
- Unitycontainer.registertype<ibird, swallow> ();
- Unitycontainer.registertype<ibird, sparrow> ();
- Ibird bird = unitycontainer.resolve<ibird> ();
- Bird. Say ();
- Console.read ();
Run it and the result:
Well, what's the situation, and why is the sparrow barking? Instead of the Swallow's bark? Originally
When an interface has multiple implementations and is not distinguished by aliases, the last injected implementation is chosen;
Add aliases to each injection below:
[CSharp]View Plaincopy
- Instantiate a controller
- Iunitycontainer UnityContainer = new UnityContainer ();
- Implementing injection, using aliases to differentiate between implementations
- Unitycontainer.registertype<ibird, swallow> ("Swallow");
- Unitycontainer.registertype<ibird, sparrow> ("Sparrow");
- Ibird swallow = unitycontainer.resolve<ibird> ("Swallow");
- Ibird Sparrow = unitycontainer.resolve<ibird> ("Sparrow");
- Swallow. Say ();
- Sparrow. Say ();
- Console.read ();
Operation Result:
This is the result we want, haha .....
When an interface has multiple implementations, it needs to be distinguished by an alias.
(ii) Unity's constructor injection
Create a new Ibirdhome interface and implement the interface:
[CSharp]View Plaincopy
- <summary>
- The home of the bird
- </summary>
- Public Interface Ibirdhome
- {
- Ibird Swallow { get; set;}
- }
- <summary>
- The home of the bird
- </summary>
- Public class Birdhome:ibirdhome
- {
- Public Ibird Swallow { get; set;}
- Public birdhome (ibird bird)
- {
- This . Swallow = bird;
- }
- }
The main method is as follows:
[CSharp]View Plaincopy
- Instantiate a controller
- Iunitycontainer UnityContainer = new UnityContainer ();
- Implementation injection
- Unitycontainer.registertype<ibird, swallow> ();
- Unitycontainer.registertype<ibirdhome, birdhome> ();
- Ibirdhome birdhome = unitycontainer.resolve<ibirdhome> ();
- BirdHome.Swallow.Say ();
- Console.read ();
Operation Result:
We just got a ibirdhome implementation through unity, but we didn't instantiate the Ibird swallow {get; set;}, but the results have been run and there are no errors.
It turns out that unity has helped us do this and we can steal a lazy one. Hey ...
(iii) attribute injection
Remove the middle constructor of the Birdhome class and add the [Dependency] attribute to the attribute
[CSharp]View Plaincopy
- <summary>
- The home of the bird
- </summary>
- Public class Birdhome:ibirdhome
- {
- // <summary>
- /// attribute injection
- // </summary>
- [Dependency]
- Public Ibird Swallow { get; set;}
- }
The result is the same as the result, pro, don't believe, try!
(iv) Initial injection of the device (name from which it originated)
The initial renderer injection is similar to the constructor injection, but does not need to be written into the constructor, but instead adds the [Injectionmethod] feature to the initial method of the initialization.
[CSharp]View Plaincopy
- // <summary>
- /// initializer injection
- // </summary>
- /// <param name= "Bird" ></param>
- [Injectionmethod]
- public void Initialize (ibird bird)
- {
- This . Swallow = bird;
- }
The result of the operation remains:
Welcome everyone to come and shoot bricks ...!
Life is endless, programming not only!
Simple application of Unity IOC container (RPM)