Microsoft's P & P team released Unity'sCommunityPreview version. You can view the latest information on the unity site.
From today on, I will join you in learning how to use this inversion of control, IOC, and dependency injection (DI) container. For IOC and Di, refer to IOC container and dependency injection mode by Martin Fowler.
Description
Unity ApplicationProgramUnity is a lightweight and scalable dependency injection container that supports constructor, attribute, and method call injection. It provides developers with the following benefits:
- This simplifies object creation, especially hierarchical object structures and dependencies.
- Allows developers to specify dependency requirement abstraction during runtime or configuration, and simplifies management of cross-concern.
- Service positioning allows customersCodeSave or cache the container. This is particularly useful when developers can persistently send containers to ASP. NET sessions or ASP. NET web applications in applications.
Common scenarios
In addition to the components that independently address cross-cutting concerns such as logs, authentication, authorization, caching, and exception handling, modern Business systems generally consist of custom business objects and components that complete special or general tasks in applications.
The key to successfully building such an application is to obtain a decoupled or extremely loosely coupled design. Loosely Coupled applications are more flexible and easy to maintain. At the same time, tests can be conducted during development to simulate the object's pile (implementation of lightweight simulation), which enhances the Actual dependency. For example, database connection, network connection, ERP connection, and rich user interface components.
Dependency injection is a major technique used to build loosely coupled applications. It provides methods to process dependencies between objects. For example, an object that processes user information may depend on accessing data storage, verifying information, and checking
Whether the user is authorized to execute updates to other objects. The dependency Injection Technology ensures that the user class correctly initializes and assembles all these objects, especially the dependency is abstract.
Using containers can have many advantages, but it will change the design method of the application, especially for Component-Based Development. Friends can choose to use it.
Basic Code
The following code shows the basic usage of the Unity container.
Iunitycontainer mycontainer = New Unitycontainer ();
Mycontainer. Register < Imyobject, myrealobject > ();
Imyobject myrealobjectinstance = Mycontainer. Get < Imyobject > ();
In the above Code, first register an imyobject interface to myrealobject using the container's Register Method
Type ing of specific classes (Note: ing can also be specified through the configuration file), and then get imyobject through the get method.
The instance of the specific object corresponding to the default ing of the interface. Here, the imyobject interface is used as the ing key. The get method uses it to find the registered instance of a specific class.
Constructor injection codeExample
Suppose we have the following class definitions:
Public Class Myobject {
Public Myobject (mydependentclass myinstance ){
Myinstance. Username = Myinstance. tostring ();
Console. writeline ( String . Format ( " My name is {0 }. " , Myinstance. username ));
}
}
Then, the container can be used to instantiate the object of the myobject class as follows:
Iunitycontainer ucontainer = New Unitycontainer ();
Myobject myinstance = Ucontainer. Get < Myobject > ();
The Unity container automatically creates an instance of the mydependentclass class for the myobject object.
Sample Code for property (configurator) Injection
For example, we have the following class definitions:
Public Class Myobject {
Private Someotherobject _ dependentobject;
[Dependency]
PublicSomeotherobject dependentobject {
Get{Return_ Dependentobject ;}
Set{_ Dependentobject=Value ;}
}
}
Then, using the same instantiation code as constructor injection, the Unity container will automatically create an object instance of someotherobject for the myobject object.
Iunitycontainer ucontainer = New Unitycontainer ();
Myobject myinstance = Ucontainer. Get < Myobject > ();
Console. writeline ( String . Format ( " Property injection object is {0 }. " , Myinstance. dependentobject ));
Features of Unity application blocks
- It provides a mechanism for building (or combining) object instances. Objects may contain other dependent object instances.
- It exposes the register method that supports container configuration using ing and objects (including Singleton instances), and returns the get method for building object instances that can contain any dependent objects.
- Control inversion (IOC) is provided by injecting pre-configured objects to classes built by the program block. Developers can specify the interface or class type (constructor injection) in the constructor, or use features to describe attributes and methods to initialize property injection and method call injection.
- Provides an inheritance level for containers. The container may have sub-containers that allow the query of object positioning to be passed from the sub-container to the parent container.
- You can read configuration information from standard configuration systems such as XML files and use it to prepare and configure containers.
- Object class definition is not required. You do not need to use features to describe the class (except for using attributes or method call injection). There are no restrictions on class declarations.
- Supports custom container extensions that developers can implement. For example, allow other object building methods and container features, such as caching.
Code download
Here, you can download the sample code in this article. For the latest information, please pay attention to my ideals and beauty.
In addition, the Enterprise Library 3.1 Chinese documents have been translated. For details, see http://wiki.entlib.net.cn/?iselibraryhelp31.ashx.
Hope to help you!
Deng Ming