Unity, a dependency injection developed by Microsoft, is expected to be heard by many people. However, it seems that there are very few Chinese tutorials now. Alas, I am not good at E .. Learn only a bit.
The company's website uses Unity for dependency injection. Here we will share a small Demo, hoping that you will have an understanding of Unity. Although Spring. Net is also quite useful,
However, I am very familiar with Microsoft, so I still use Microsoft.
1. First of all, download Unity first. If you cannot find it, you can search by google.
2. Add Microsoft. Practices. Unity. dll and Microsoft. Practices. Unity. Configuration. dll references to the Web project.
3. Add the following nodes to configSections in the web. config file:
<Section name = "unity" type = "Microsoft. practices. unity. configuration. unityConfigurationSection, Microsoft. practices. unity. configuration, Version = 1.1.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
4. Add the following nodes to configuration in the web. config file:
<! -- Specific Untiy configuration items -->
<Unity>
<Containers>
<Container name = "service">
<Types>
<Type = "testunity. itest, testunity" mapto = "testunity. Test, testunity"/>
</Types>
</Container>
<Container name = "Repository">
<Types>
<Type = "TestUnity. IRepo, TestUnity" mapTo = "TestUnity. Repo, TestUnity"/>
</Types>
</Container>
</Containers>
</Unity>
<! -- End untiy -->
The name in the container is random, and the type in the type is the corresponding interface. To add the namespace, mapTo is the implementation class corresponding to the interface.
Then add a class UnityContext, as shown below:
Public class UnityContext
{
Public static UnityContext _ instance;
Private UnityContainer _ container;
Public static UnityContext Instance
{
Get
{
If (_ instance = null) _ instance = new UnityContext ();
Return _ instance;
}
}
/// <Summary>
/// Constructor
/// </Summary>
Public UnityContext ()
{
_ Container = new UnityContainer ();
UnityConfigurationSection setion = (UnityConfigurationSection) ConfigurationManager. GetSection ("unity ");
Setion. Containers ["Service"]. Configure (_ container );
Setion. Containers ["Repository"]. Configure (_ container );
}
Public T Load <T> ()
{
Return _ container. Resolve <T> ();
}
}
This is a simple Singleton Mode Management Unity class... It's a little bad... "Service" and "Repository" are the name attributes of the container in the configuration file.
The public T Load <T> () method returns the implementation object of the specified type of interface.
The call is as follows:
ITest test = UnityContext. Instance. Load <ITest> ();
IRepo repo = UnityContext. Instance. Load <IRepo> ();
String hell = test. SayHello ();
String hi = repo. SayHi ();
Among them, ITest, Test, IRepo, and Repo are interfaces and corresponding implementation classes, and only a "Hello" or "Hi" method is returned simply. Only for testing.
In this way, it is basically done .. As for the benefits, I am not quite sure, maybe it is to reduce coupling .. There are still many things you don't understand. This is a poor write, but I hope it will inspire you. I hope you will give me more advice on any shortcomings or mistakes.
The following is my use:
1.Asp.net MVC Framework
2. LinQ to SQL framework
3. Unity1.1