[Original] native IOC framework under unity-. net, allowing some people to use it first

Source: Internet
Author: User

By chance, Microsoft also developed the IOC framework, which belongs to the Microsoft patterns & Practices System and is called unity. It may be used in the current project, as a result, I downloaded and tested the main functions. It seems to be a little more frustrating than spring, but there is no way. Many of them have Microsoft's cleanliness, except for Microsoft's framework, other users feel uneasy. Well, let's talk less about it. Go!

By the way, I don't know how to use IOC for programming in Unity quickstarts. If IOC is used in this way, so it seems that Microsoft is not going to advocate the concept of "non-intrusive... this article introduces how to use unity in the configuration file mode. Because E is too bad, the Unity document is read-only. If there is an error in the example, correct it.

1. Download unity and install it...

2. Add Microsoft. Practices. Unity. dll and Microsoft. Practices. Unity. configuration. dll references to the project.

III. for Windows and console projects, add an app. config File, web projects use web. config is ready. This example uses the console Project (it is said that experts like to use the console. I am not a master, just install it, ~).

4. Make app. config look like the following:

 

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Configsections >
< Section Name = "Unity"
Type = "Microsoft. Practices. Unity. configuration. unityconfigurationsection,
Microsoft. Practices. Unity. configuration, version = 1.1.0.0,
Culture = neutral, publickeytoken = 31bf3856ad364e35"   />
</ Configsections >
< Unity >
< Typealiases >
< Typealias Alias = "Singleton" Type = "Microsoft. Practices. Unity. containercontrolledlifetimemanager, Microsoft. Practices. Unity" />
< Typealias Alias = "Isayhello2" Type = "Cinlap. Study. unitydemo. isayhello2, cinlap. Study. unitydemo"   />
< Typealias Alias = "Sayhello2acloud" Type = "Cinlap. Study. unitydemo. sayhello2acloud, cinlap. Study. unitydemo"   />
< Typealias Alias = "Sayhello2think8848" Type = "Cinlap. Study. unitydemo. sayhello2think8848, cinlap. Study. unitydemo"   />
< Typealias Alias = "Isingletondemo" Type = "Cinlap. Study. unitydemo. isingletondemo, cinlap. Study. unitydemo"   />
< Typealias Alias = "Singletondemoimpl" Type = "Cinlap. Study. unitydemo. singletondemoimpl, cinlap. Study. unitydemo"   />
</ Typealiases >
< Containers >
< Container Name = "Containerone" >
< Types >
< Type Type = "Isayhello2" Mapto = "Sayhello2acloud" Name = "Acloud" />
< Type Type = "Isayhello2" Mapto = "Sayhello2think8848" Name = "Think8848" />
< Type Type = "Isingletondemo" Mapto = "Singletondemoimpl" Name = "Singletondemoimpl"   >
< Lifetime Type = "Singleton"   />
</ Type >
</ Types >
<! -- <Instances>
</Instances>
<Extensions>
</Extensions>
<Extensionconfig>
</Extensionconfig> -->
</ Container >
</ Containers >
</ Unity >
</ Configuration >

 

Here we need to explain something, otherwise it will be a bit confusing.

The typealias node is used to create an alias for a type name that is not easy to remember (it seems a bit nonsense), so that a type name with many characters can be changed to a short point.

The container node defines the container for managing dependency and lifecycle (IOC container concept). A name must be provided in the configuration.

The Type node can be seen from its mapto attribute. It provides a specific definition of dependency and lifecycle.

Intercept questions (a conventional talk): We consider an application scenario where R & D often listens to the design review: "When I design, it was originally thought ..., as a result, due to the tight schedule of the project, I had to first meet the requirements of XX Company, without considering the universality... "Or:" I thought our product running (installation) Environment (process) would not change much. I didn't expect it... ", often at this time boss will be born with those consequences are not very serious, but in the end, often only the hope of the next version or the next project, but this process is often repeated next time...

Session 1 (about architecture): an article on csdnArticleI was deeply impressed once: A CEO said that their system has been running for more than 40 years, and what supports the system's life is a reasonable system architecture. This sentence has made me think for a long time. What kind of architecture can make a system so powerful? In the past, we saw that the system was basically eliminated due to changes in the operating environment or load. We seldom heard that the system has lived for more than ten years, and the company's system can run for more than 40 years !!! After thinking deeply, we come to a simple conclusion that the company's system has applied a reasonable architecture from the very beginning and followed the correct design theory in a timely manner. Therefore, the process is easily evolved into an object, objects are easily evolved into components, and components are easily evolved into services (maybe it is like this ). Therefore, I am determined not to add those self-righteous assumptions in my design process: it is impossible to transplant across platforms, it is impossible to expand functions, it is impossible to increase load, and it is impossible...

Question 2 (about IOC): Someone may ask, I can write it again after the requirement changes.CodeYeah? I have a wide range of control (Group) components, I have a powerful IDE, and it takes a long time for me to modify the current system based on the new requirements of the customer... that's right. That's right. This article is not suitable for you. Some people may ask, IOC seems to be no different from factory. Why do I have to spend time learning it? factory is really great. I used to be complacent in the factory mode, but later I found out what are the disadvantages of the factory. Why should I open the project and modify the instance type?Source codeWhat about it? Someone says, won't you use the configuration file? Configuration file? It sounds good, but what if I want to implement Singleton, specify the initial Instance value, or? Someone in the IOC Framework Group has already sneaked into a smile. Look, let's use our products...

The digress is over.

5. Create a project and add the following files to it:

Isayhello2.cs

 

Public   Interface Isayhello2
{
String Sayhello ();
}

 

Sayhello2think8848. CS

 

Public   Class Sayhello2think8848: isayhello2
{
# Region Isayhello2 Member

Public StringSayhello ()
{
Return "Hello think8848!";
}

# Endregion
}

 

Sayhello2acloud. CS

 

Public   Class Sayhello2acloud: isayhello2
{
# Region Isayhello2 Member

Public StringSayhello ()
{
Return "Hello acloud!";
}

# Endregion
}

 

Isingletondemo. CS

 

Public   Interface Isingletondemo
{
Datetime getinittime ();
}

 

Singletondemoimpl. CS

 

Public   Class Singletondemoimpl: isingletondemo
{
Private Datetime time;

PublicSingletondemoimpl ()
{
This. Time=Datetime. now;
}

# RegionIsingletondemo Member

PublicDatetime getinittime ()
{
Return This. Time;
}

# Endregion
}

 

6. Modify the console's program. CS file to make the main method look like the following:

 

Static   Void Main ()
{
Unitycontainer mycontainer =   New Unitycontainer ();
Unityconfigurationsection Section = (Unityconfigurationsection) configurationmanager. getsection ( " Unity " );
Section. containers [ " Containerone " ]. Configure (mycontainer );

Isayhello2 sayhello2acloud = Mycontainer. Resolve < Isayhello2 > ( " Acloud " );
Isayhello2 sayhello2think8848 = Mycontainer. Resolve < Isayhello2 > ( " Think8848 " );

Console. writeline (sayhello2acloud. sayhello ());
Console. writeline (sayhello2think8848. sayhello ());

Console. writeline ( "" );
Console. writeline ( " Singleton demo " );
Isingletondemo singletondemo1 = Mycontainer. Resolve < Isingletondemo > ( " Singletondemoimpl " );
Console. writeline ( " Singletondemoimpl1 created: {0}, singletondemoimpl1.getinittime () result is {1 }: " , New   Object [] {Datetime. Now. tostring (), singletondemo1.getinittime (). tostring ()});

Thread. Sleep ( 1000 );
Isingletondemo singletondemo2 = Mycontainer. Resolve < Isingletondemo > ( " Singletondemoimpl " );
Console. writeline ( " Singletondemoimpl2 created: {0}, singletondemoimpl2.getinittime () result is {1 }: " , New   Object [] {Datetime. Now. tostring (), singletondemo1.getinittime (). tostring ()});

Console. Readline ();
}

 

VII. F5.

Result:

 

 

This article only demonstrates the most basic and commonly used functions of unity. Other functions are to be continued.

Unity has few Chinese documents. I have found a configuration file today. If you are interested, click here to read it.

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.