Definition:Unity Application Block (Unity) is a lightweight and scalable dependency injection container. The dependency between components is determined by the container at runtime, containers dynamically inject certain dependencies into components to build highly loosely coupled software tools, and inject dependencies into containers to process all the details of interrelated components, therefore, you can build an independent component to increase the probability of Component Reuse and build a flexible and scalable platform for the system.
Function:It helps build loosely coupled applicationsProgramIt provides developers with the following convenience:
1. It provides a mechanism for creating (or assembling) object instances to simplify object creation, especially when hierarchical object structures and dependencies exist.
2. It supports abstraction of requirements, which allows developers to specify dependencies at runtime or in the configuration file to simplify management of cross-concern (crosscutting concerns)
3. It is determined by pushing component configurations to containers, which increases flexibility.
4. Service positioning capability; this enables the client to store or cache containers
Note:To put it bluntly, unity is a solution for building a plug-in system. Our application consists of various functional module components, and each component is highly loosely coupled with the system through dependency. Only interfaces are reserved during development, so that components can inherit the functions of these interfaces. No matter how the application is implemented, the components are responsible. The application only calls the interface to complete the functions.
Instance:The following example will help us understand the unity dependency injection container.
The requirement is as follows: Our application has a log module, which may record logs in different places according to different requirements and deployment, such: text, database...
1. Define the log interface class:
Public Interface Ilogger
{
Bool Append ( String S );
}
2. record logs to the text implementation class ilogger Interface
Code
/// <Summary>
/// Record logs to text
/// </Summary>
Public Class Filelogger: ilogger
{
Public Bool Append ( String S)
{
// CodePartially omitted
Throw New Notimplementedexception ();
}
}
3. record logs to the database implementation class ilogger Interface
Code
/// <Summary>
/// Record logs to the database
/// </Summary>
Public Class Dblogger: ilogger
{
Public Bool Append ( String S)
{
// Code omitted
Throw New Notimplementedexception ();
}
}
4. Call
Code
Class Program
{
Static Void Main ( String [] ARGs)
{
Addlog ( " Program started " );
}
Static Public Void Addlog ( String Log)
{
// Create a container
Iunitycontainer mycontainer = New Unitycontainer ();
// get the injection object from the configuration file
unityconfigurationsection = (unityconfigurationsection) configurationmanager. getsection ( " unity " );
section. containers. default. configure (mycontainer);
// get object instance
ilogger logger = mycontainer. resolve ilogger > ();
//Call object Method
Logger. append (log );
}
}
5. configuration information
Code
< Configuration >
< Configsections >
< Section Name = "Unity" Type = "Microsoft. Practices. Unity. configuration. unityconfigurationsection, Microsoft. Practices. Unity. Configuration" />
</ Configsections >
< Unity >
< Containers >
< Container >
< Types >
<! -- <Type = "unitytest. ilogger, unitytest" mapto = "unitytest. filelogger, unitytest"/> -->
< Type Type = "Unitytest. ilogger, unitytest" Mapto = "Unitytest. dblogger, unitytest" />
</ Types >
</ Container >
</ Containers >
</ Unity >
</ Configuration >
How to record the log, where the main program does not care, it just needs to be called, the specific log implementation function is completed by the log component module, you can change the configuration fileUnityTo register the logging module.