Unity (III): Quick Start

Source: Internet
Author: User

Microsoft patterns & practices has finally released unity, which can be downloaded from here. For more information, see the release notes of Grigori Melnik, the boss of unity, but there is no more information than the download page.

Recently, the patterns & Practices team adopted a new document release mode, that is, when the release version of the product is released, the product documentation is synchronized to msdn. In this case, if you only want to know about the product first, or you want to know whether to install the product, you can browse it on msdn without installing it.

Here, we will first give a simple example of using unity. Through examples, we will have a preliminary understanding and understanding of the use of unity. In the followingArticle. If you have no idea about unity, refer to the previous two articles: Unity (1): From objectbuilder, Unity (2): What is unity ?.

This example shows a basic application of unity in type mapping.

 

To use unity, follow these steps.

1. Create a container;

2. register the ing between interfaces and classes in the container;

3. parse the correct object from the container.

 

For the following description, we first compile Several Interfaces and classes that are later required:

Step0. preparation

Ilogger interface:

Consolelogger class:

Nulllogger class:

 

Step 1. Create a container

The simplest way to create a container instance in unity is to directly create a container instance using the constructor, as shown below:CodeAs shown in:

Iunitycontainer Container = new unitycontainer ();

Step2. register the interface ing

A set of register methods are provided in unity for us to register interface mappings in the container, as shown in the following code:

Container. registertype <ilogger, consolelogger> ();

Step3. get the object instance

In unity, a set of resolve methods are provided to obtain object instances, as shown in the following code:

Ilogger logger = container. Resolve <ilogger> ();

OK, that's easy!

 

Test:

Scenario 1: register a consolelogger as the implementation of ilogger to the container, parse the implementation of ilogger from the container, and call the LOG method of ilogger.

Result:

 

Scenario 2: register an nulllogger as an ilogger implementation to the container, parse the ilogger implementation from the container, and call the ilogger LOG method.

Result:

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.