[Entlib] how to learn from Microsoft enterprise database 5.0-Step 10: Use unity to decouple your system-Part1-why should we use unity?

Source: Internet
Author: User

Today, we will continue to learn about Microsoft enterprise database 5.0. Today we will introduce the important module of Microsoft enterprise database-Unity.

This articleArticleWe will mainly introduce:

1. Basic knowledge about the unity module.

2. Why should we use unity?

3. Under what circumstances should unity be used.

4. Recommendation of Unity Learning Resources

 

1. Basic knowledge about the unity Module

In enterprise database 4.0, the patterns & Practices Team made major changes to the architecture of the entire enterprise database, and integrated the original core dependency injection framework, objectbuilder, into Untiy, before unity1.0 was released, many other projects of the entire enterprise library and P & P depend on objectbuilder, however, the complexity of learning is caused by the fact that objectbuilder is too complex and does not have detailed and standardized documents and the functions of many other projects are integrated, therefore, P & P launched its own dependency injection container -- unity, and integrated objectbuilder into it, which can smoothly take over the original dependency injection method, you can also create Enterprise Library objects. For details about unity, refer to this article: extended learning and dependency injection in the Library (rebuilding Microsoft Enterprise Library).

After introducing the origin of unity, we need two more pieces of knowledge.IOC(Inversion of Control) AndDi(Dependency Injection):

1,IOC(Inversion of Control) -- Reverse control. Based on the literal meaning, we can know that control is reversed.

Before we developedCodeIn the process, each layer is closely linked, and one layer depends on one layer. If a layer changes, other layers may also have a chain reaction, for example, the business logic layer (BLL) dependent on the data access layer (DAL), usually directly create the corresponding objects of the data access layer, such:

 
Public class usermanage {userservice Serivce = new userservice (); Public void insert (User user) {service. insert (User );}}

As you can see, in the above Code, the business logic layer performs operations on the data access layer directly through instantiation. At first glance, there is no problem, but when the project reaches a certain scale, at the same time, when the business logic changes (such as when it depends on other data access layers or special data access layers), these codes are hidden bombs, once the explosion, you will be tempted to rewrite the code.

The emergence of IOC is precisely to solve this problem. Friends who have used the factory model will surely know that, we can use specific configurations or conventions to reflect the loading or creation of required object instances based on specific situations. IOC is an upgraded version of the factory mode, IOC takes over the ing between these complex layers and layers, interfaces and classes, or classes and classes. We don't need to consider what I need when writing code, just tell IOC what I want, IOC will provide the objects we need according to our configuration or conventions.

Unity is such an excellent and lightweight IOC container. We can configure the relationship between objects through code or xml configuration files, you can call the unity container directly at runtime to obtain the required objects.

2,Di(Dependency Injection), Dependency injection. Literally, it means Injection Based on dependency.

Dependency injection includes constructor injection, property injection, and method injection.

For a class object, the three Members that are mainly exposed to other objects are constructors, attributes, and methods. These three class members can all be dependent on other class or interface parameters. For example:

 
Public Class A {public A (B) {This. B = B;} public B {Get; set;} public void test (B) {console. writeline (B. tostring ());}}

In the above Code, constructor, attribute, and method in Class A all have dependencies on Class B objects. In general, if you want to instantiate Class, the B object must be passed, but if the B object is null, an exception will occur if you directly perform operations on Attribute B in the following code. Similarly, if you call the test method, passing a null value of B will also cause an exception. Our general practice is to build a B object before calling and then pass it in, however, this will lead to code duplication, which reflects the importance of Di. We can use IOC to create these dependencies, in this way, we do not need to think about creating the called object dependency before calling. We only need to configure the ing in advance, which greatly facilitates our daily development.

In general, Di is the way to implement IOC. The idea of IOC is to remove the dependency between objects, which is controlled by IOC, while Di is the specific implementation of IOC.

 

2. Why should we use unity?

There are already many excellent IOC containers on the. NET platform, such as spring. net,

However, as a lightweight IOC container launched by the P & P team, since unity1.0 was released with entlib4.0, it has been maintained to 2.0 and has been well-developed and used by many developers, it also plays an important role. The main project is the enterperise library developed by P & P.

Why should I use unity? It also illustrates the advantages of unity, or the reasons for using unity. The following is my translation (there must be a translation error. If you are good at English, you can view the original article directly ):

1. Unity supports Simple Object creation, especially hierarchical object structure and dependency, to simplifyProgramCode. It contains an instance mechanism for compiling objects that may be dependent on other objects.

2. Unity supports necessary abstraction. It allows developers to specify dependencies at runtime or configuration, and easily manage cross-node (AOP ).

  • 3. Unity adds the flexibility to delay to container component configuration. It also supports a container hierarchy.
  • 4. Unity has the service locating capability. It is very useful for a program to reuse components to separate and centralize functions in many cases.
    • 5. Unity allows the client to store or cache containers. It is particularly effective for developers in ASP. NET web applications to persist containers in sessions or applications in ASP. NET.

      • 6. Unity has the Interception Capability. It allows developers to add a function to an existing component by creating and executing handlers (before the method or attribute is called, and return the call result again.

        • 7. Unity can read configuration information from the standard configuration system, for example, XML files, and use the configuration file to configure containers.

          • 8. Unity supports developers to implement custom container extensions. For example, you can implement methods to allow additional Object Structures and container features, such as caching.

            • 9. Unity allows architects and developers to easily implement general design patterns in modern programs.

You can also view -- [translation] What does unity do? -- What can unity do? Learn more about what unity can do.

 

Iii. Under what circumstances should unity be used

In the official document, when should I use unity? There are already more detailed and more standard statements in, the following is my translation (there must be a translation error, good English can directly view the original ):

  • 1. The built system relies on sound object-oriented principles, but a large number of different codes are intertwined and difficult to maintain.
  • 2. The constructed objects and classes must depend on other objects or classes.
  • 3. dependent on complex or abstract objects.
  • 4. We hope to take advantage of constructor, method, or attribute call injection.
  • 5. You want to manage the lifecycle of an object instance.
  • 6. You want to manage and change dependencies at runtime.
  • 7. You want to generate a policy chain or pipeline processing container to implement cross-cutting (AOP) tasks when intercepting method or attribute calls.
  • 8. It is hoped that the dependency can be cached or persisted during the sending back operation in the Web application.

At the same time, the document also pointed out under what circumstances do not need to use unity, and I think it is also important:

1. The written object or class does not depend on other objects or classes.

2. The dependency is too simple or does not need to be abstracted.

Of course, for most enterprise-level development, we recommend that you use unity to take over object creation and dependency, so as to ensure good system coupling.

 

Iv. Recommendation of Unity Learning Resources

Since its official release in, unity has already had many excellent learning resources. The following are some of my preferred learning resources:

1. unity2.0 official documentation (relatively speaking, you can also directly download this document EntLib50-combined, which contains the introduction of all modules of the Enterprise Library and API)

2. Hands-on labs for Microsoft Enterprise Library 5.0 (Official example of entlib, which includes functional examples of each module of entlib, also contains unity2.0)

3. unity2.0 source code

4. Articles about unity written by artech

 

The above is all the content in this article. This article is based on the official unity documents and some personal understandings. If there are any errors, you are welcome to point out.

 

Index of a series of articles on the learning path of Microsoft enterprise database 5.0:

Step 1: getting started

Step 2: Use the vs2010 + data access module to create a multi-database project

Step 3: Add exception handling to the project (record to the database using custom extension)

Step 4: Use the cache to improve the website's performance (entlib caching)

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 2

Step 6: Use the validation module for server-side data verification

Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 1

Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 2

Step 8. Use the configuration setting module and other methods to classify and manage enterprise database configuration information

Step 9: Use the policyinjection module for AOP-PART1-basic usage

Step 9: Use the policyinjection module for AOP-PART2-custom matching rule

Step 9: Use the policyinjection module for AOP-PART3 -- Introduction to built-in call Handler

Step 9: Use the policyinjection module for AOP-PART4 -- create a custom call handler to achieve user operation Logging

Step 10: Use unity to decouple your system-Part1-Why use unity?

Step 10: Use unity to decouple your system-Part2-learn how to use Unity (1)

Step 10. Use unity to decouple your system-Part2-learn how to use Unity (2)

Step 10: Use unity to decouple your system-Part2-learn how to use Unity (3)

Step 10: Use unity to decouple your system-Part3-dependency Injection

Extended learning:

Extended learning and dependency injection in libraries (rebuilding Microsoft Enterprise Library) [go]

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.