You can use Unity Application Block (Unity) to intercept the objects in the Enterprise Library before or after functions are run, generate these objects, and inject them into other objects.
The Enterprise Library contains many classes that allow you to obtain instances defined in the Enterprise Library configuration from the Unity container, such as SqlDatabase or CacheManager objects. In addition, some good injections replace the previous static methods. Using Unity dependency injection can simplify your code.
4.1 of system requirements
Windows xp, win7, vista
. Net framework 3.5 or later
VS208
I. Framework dependency
To be accurate, it is the dependency of the Assembly, that is, all the functions to be implemented must be referenced by those assembly.
1. Assembly on which all functions depend
1.1 Microsoft. practices. enterpriseLibrary. common. dl, which contains the core services of the Enterprise Library and is shared by other modules of the Enterprise Library (except for Unity, you can use the Unity Application Block to implement dependency injection function separately ).
1.2 Microsoft. Practices. ObjectBuilder2.dll, object creation subsystem, which executes all repetitive and necessary work tasks related to object creation and collection, and provides high-level flexibility.
2. Assembly on which the cache module depends
2.1 if you use a database as the back-range Data storage, you need the Data Access module.
2.2 If You Want to encrypt the data in the cache, you need the Cryptoraphy module.
3. Additional assembly required for Exception Handling
3. 1 If you want to use the Logging module to record exception information, you need to reference Microsoft. Practices. EnterpriseLibrary. Logging. dll and Microsoft. Practices. EnterpriseLibrary. ExceptionHandling. Logging. dll.
3.2 you may also need other assemblies, such as the Data Access module, because you may want to record exception information.
4. Policy Injection additional assembly required by Policy Injection
4.1 Policy Injection provides a standard processing process. If your code does not use built-in processing, you can not deploy the Assembly on which policy injection depends.
4.2 if you want to use the built-in policy injection permission processing, you need to add the Security Module
4.3 if you use the built-in policy injection for exception handling, you need to add the Exception Handling Module
4.4 If you use the policy injection built-in log processing, you need to add the Log Module
4.5 if you use the built-in data verification function of policy injection, you need to add a verification module.
5. Additional security module dependent assembly
5.1 cache module. The security module uses the cache module to cache security information. You can also use your own cache Provider to replace the cache module in the Enterprise Library. When configuring the cache module, you may also need data access modules. For more information, you can view the dependencies of the cache module.
Ii. Use of the dependency injection module
Unity Applicaiton Block provides a lightweight and scalable dependency injection container that supports constructor, attribute, and method call injection. You can use Unity or custom dependency injection in your applications to create enterprise database objects or custom business objects or services dependent on other objects or services.
Alternatively, you can create objects in the same way as the enterprise database.
If you decide to use Unity, you need to add dependencies on this Assembly. In addition, like other modules, Unity depends on ObjectBuilder. For more information and dependencies, you can view the dependency diagram.
Iii. Dependency Graph
There are many ways to create an object instance for the enterprise database. You can use one of the following methods to achieve this purpose;
- Create an object instance using the factory class of each module and the core class library of the Enterprise Library
- Use the Unity module to create any types of object instances
- Use the Unity or third-party dependency injection method to create an object instance without using the enterprise library CORE class library.
1. Factory method for using enterprise database
Enterprise database 4.1 supports object creation methods of previous versions. Each function module and core library can use the factory method to create an object instance. For example, you can use the following code to create a configured CacheManager class instance.
CacheManager diskCache = CacheFactory. GetCacheManager ("name ");
The above code is common in the core class libraries of the Enterprise Library. It directly calls the ObjectBuilder tool class. In, you will find that all functions depend on the core and ObjectBuilder of the enterprise database.
To create an object using this method, you must deploy the following Assembly:
- Core library Microsoft. Practices. EnterpriseLibrary. Common. dll
- Object creation Assembly Microsoft. Practices. ObjectBuilder2.dll
2. Use Unity Application Block to create an object
Unity allows developers to create various objects in dependency injection mode. If necessary, the system automatically injects the dependent objects. You can use this method to create enterprise database objects and custom business objects. Shows how to use Unity to change the dependency between applications and the core of the Enterprise Library.
In this case, except Unity, all modules depend on the Configuration System of the core class library. However, although Unity uses the configuration information in the app. config and web. config files stored in the application directory, the Unity container is not automatically created and configured. On the contrary, developers need to create containers and load configuration information.
After the container is created and configured, developers can use the following syntax to create objects.
CacheManager diskCache = myContainer. Resolve <CacheManager> ("name ");
To use this technology to create objects, developers need to deploy the following Assembly:
- Core class library Microsoft. Practices. EnterpriseLibrary. Common. dll
- Unity Assembly Microsoft. Practices. Unity. dll
- Object creation Assembly Microsoft. Practices. ObjectBuiler2.dll
- Assembly of other modules used in the Application
3. Use third-party dependency Injection Technology
Unity is designed as a dependency injection technology that can be used in the Enterprise Library or separately. In addition, if necessary, developers can use a third-party dependency injection framework to replace Unity. This shows the dependencies between modules when a third-party dependency injection framework is used.
This method requires that the third-party dependency injection framework support Describing Objects, settings, and necessary relationships with functional modules in the configuration. Unity contains a standard configuration template that uses the dependency Injection Technology in the Enterprise Library.
Using this technology to create objects, developers need to deploy the following Assembly:
- The Assembly of the functional modules used.
- The Common Assembly of the Enterprise Library, which includes core shared Functions
- To use Unity as the dependency injection technology, developers must deploy Microsoft. Practices. Unity. dll and Microsoft. Practices. ObjectBuilder2.dll.
- If you use third-party dependency injection technology, developers need to deploy the relevant Assembly and configure them.
Sometimes, if the required assembly is not added, a prompt will be prompted during compilation, prompting the assembly to be added.
Not complete ..................