Simple Application of Unity IOC container

Source: Internet
Author: User

Unity is a lightweight and scalable dependency injection container implemented by the Microsoft patterns & Practices Group in C #. It facilitates developers to build loosely coupled applications,

It has the following advantages:

1. simplified object creation, especially for hierarchical object structures and dependencies;

2. Abstract requirements: allows developers to specify dependencies at runtime or in the configuration file to simplify management of cross-concern points;

3. delayed the time to configure components for containers and increased flexibility;

4. Service positioning capability, enabling customers to store or cache containers;

5. instance and type Interception

: Http://unity.codeplex.com/

The latest version is unity 3.0 for. Net 3.5 preview.

The following programs use version 2.1


Start the unity tour


(1) My First Unity demo


Create a console application and reference the Microsoft. Practices. Unity. dll file;

Create a new bird interface and define a bird call method;

/// <Summary> /// bird interface /// </Summary> Public interface iBIRD {// <summary> /// speech /// </Summary> void say ();}

Implement this interface:

/// <Summary> /// swallow /// </Summary> public class swallow: iBIRD {public void say () {console. writeline ("Swallow is calling... ");}}

Implement IOC inversion control through unity in the mian method;

Static void main (string [] ARGs) {// instantiate a controller iunitycontainer unitycontainer = new unitycontainer (); // inject unitycontainer. registertype <iBIRD, swallow> (); iBIRD bird = unitycontainer. resolve <iBIRD> (); bird. say (); console. read ();}

Running result:


This small instance has implemented a simple IOC control inversion.

What if an interface has two implementations? Is it just adding a code similar to the following? Next interview.

Unitycontainer. registertype <iBIRD, swallow> (); in the original program, we add a sparrow class to implement the iBIRD interface:


Public class Sparrow: iBIRD {public void say () {console. writeline ("Sparrow is called ....");}}


Main method code:

// Instantiate a controller iunitycontainer unitycontainer = new unitycontainer (); // inject unitycontainer. registertype <iBIRD, swallow> (); unitycontainer. registertype <iBIRD, Sparrow> (); iBIRD bird = unitycontainer. resolve <iBIRD> (); bird. say (); console. read ();

Run the command and the result is as follows:


Hmm? What is the situation? Why is the sparrow calling... not the swallow? Original

When an interface has multiple implementations and does not use an alias, the last injection implementation is selected;


The following alias is added for each injection:

// Instantiate a controller iunitycontainer unitycontainer = new unitycontainer (); // implement injection and implement unitycontainer using aliases. 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 ();


Running result:

This is what we want, haha .....

When an interface has multiple implementations, it must be distinguished by aliases.


(2) constructor injection of Unity


Create an ibirdhome interface and implement it:

/// <Summary> /// Birdie's home /// </Summary> Public interface ibirdhome {iBIRD swallow {Get; set ;}} /// <summary> // Birdie's home /// </Summary> public class birdhome: ibirdhome {public iBIRD swallow {Get; set;} public birdhome (iBIRD bird) {This. swallow = bird ;}}


The main method is as follows:

// Instantiate a controller iunitycontainer unitycontainer = new unitycontainer (); // inject unitycontainer. registertype <iBIRD, swallow> (); unitycontainer. registertype <ibirdhome, birdhome> (); ibirdhome birdhome = unitycontainer. resolve <ibirdhome> (); birdhome. swallow. say (); console. read ();


Running result:

We only get an ibirdhome implementation through unity, but we didn't instantiate iBIRD swallow {Get; set;}, but the result has been run and no error is reported.

It turns out that unity has helped us with these tasks, so we can be lazy .......


(3) Property Injection


Remove the constructor from the birdhome class and add the [dependency] feature to the attribute.

/// <Summary> /// Birdie's home /// </Summary> public class birdhome: ibirdhome {// <summary> // property injection /// </Summary> [dependency] public iBIRD swallow {Get; set ;}}

The running result is the same as the preceding result. Do not believe it. Try it!


(4) Early enator Injection(Self-defined name)

The early-time generator injection is similar to the constructor injection, but does not need to be written into the constructor. Instead, the [injectionmethod] feature is added to the initial method.

/// <Summary> /// initialization machine injection /// </Summary> /// <Param name = "bird"> </param> [injectionmethod] public void initialize (iBIRD bird) {This. swallow = bird ;}


The running result is still:



Welcome to make a brick ........!


Endless life, programming!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.