To create the key to a harmonious society: Dependency Injection

Source: Internet
Author: User
Tags constructor implement reflection requires

The development of history

Ancient Matriarchal clan, everyone is an independent individual, what tools need to polish a tool, they need to understand all the process to survive. For example, hunting, from the early preparation of ropes, sharp wood, to the middle of the trap, late harvest, need to understand very thoroughly. The corresponding programming is the new Rope (), New sharp Wood (), New Trap (), New X (). Instantiate all the required resources and then proceed to the logical process.

Mankind is gradually progressing, the industrial revolution has changed the structure of the whole society. People do not need to understand all the process, just go to a factory or procurement platform, input the things you want, you can get. Corresponding programming is the factory model, the need for a static factory class, an abstract product type class, a you want to get the product category can be like, since then entered the national Taobao era, what to buy.

When you buy a ladder to build a light bulb, but when it is repaired, how to deal with the ladder becomes a problem, throwing away, do not throw away second-hand and very troublesome. This time need our protagonist: Harmonious Society debut! Advocate not extravagant not waste, this is a recycling mechanism, you need it just to say, seconds seconds to your hands, you do not need to know where he came from. Do not need you also do not have to control, I direct seconds second again to go. Is there a magic feeling? This is dependency injection! Dependency injection unlocks the dependencies of objects and objects and requires other objects to be injected directly to you, and you don't need to. Objects conform to the OCP principle (open externally, closed for modification), and the container is responsible for all relationship matching. In this layer, the container is the rule of the society, and the object only needs to care about what part of it is done. Easy and cozy!

Dependency Injection: Dependency injection

From initial new,new,new to later polymorphism, interface-oriented programming, matching the rules stipulated by the Protocol, until on demand, external injection of control reversal dependency injection. In fact it is the same as the logic of human progress, and the goal is to make assignments more explicit, more efficient, and less dependent on each other. Dependency injection isolates the changes so that they do not affect the invariant part of the object. Dependency injection is actually a combination of many principles. Polymorphism makes classes no longer dependent on other service classes, requiring only interfaces to implement OCP (externally extended open, closed for modification). In order not to need an instance specific service class, we need to define the injection point (interface), the container to carry on the service, realizes the injection.

Injection container: Container

Dependency injection requires a container to store all of the resources, according to your needs on demand, to inject. Dependency Injection also has a name called control reversal, which is often said of the IOC.

Lightweight there are Unity Container, AUTOFAC, spring and so on, the heavyweight has EJB, and the drift between the weight is jboss and so on. The following series will detail several commonly used, such as CONTAINER,AUTOFAC,EJB.

Because I often use prism,orchard, so will introduce more detailed container and AUTOFAC, will also use the experience to share more to everybody.

Basic Technology: Reflection

The IOC is the sublimation of the factory model, the most basic technology is "reflection". Flexible injection based on an injected matching table or rule.

Injection rule:

1. XML to be configured.

Spring uses an XML file to match beans. The advantage is that it completely pulls away from the code hierarchy, configures it in XML, and uses a string for reflection injection. But the downside is the maintenance of XML, which requires a good IDE

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

2.Assembly injection, Class,interface match. In. NET individuals prefer this.

var builder = new Containerbuilder ();   
      Builder. Registergeneric (typeof (Dal<>)). As (typeof (Idal<>)). Instanceperdependency ();   
Builder. Registergeneric (typeof (Repository<>)). As (typeof (Irepository<>)). Instanceperdependency ();   
Builder. Register (c=>new persionbll (irepository<persion>). Resolve (typeof (Irepository<persion>)));   
Builder. Register (c => new CUSTOMBLL (irepository<custom>)   
    C.resolve (typeof (Irepository<custom>)));   
    var container = Builder. Build ()

Only a few examples are listed here. Specific will be introduced in detail in the series after the chapter.

Injection method:

1. Interface injection, 2. Constructor injection, 3. Attribute injection

Open dishes (not using containers, using interfaces and implementation classes only to show the basic logic of Dependency injection)

Scene: Home a lot of TV, sometimes this can be used, sometimes that can be used. Hope that in the replacement, the individual side is no need to change anything, still pick up the remote can watch TV, no matter which.

Solution: (Interface Injection)

Create interface public interface Itvprovider {void Watchtv () to realize TV viewing function;   
//Create an entity class for the TV, inherit Itvprovider, and implement its Watchtv method. public class Konkatv:itvprovider {public void Watchtv () {///realize the logical generation of the Konka TV function Code}//Create an injection interface to implement functions like container/xml linked object matching public interface Itvinjecter {void Inj   
Ecttv (Itvprovider tvprovider); //Create a watch TV control, inherit Itvinjecter (the container is inherited like idepency this can get to the container, here is not using the container method) public controls Watchtvcontrol:itvinjec   
       
        ter {private Itvprovider _tvprovider;   
        If you use a container, you can implement the injection in the constructor, without having to write the injection method manually.   
       
        public void Injecttv (Itvprovider tvprovider) {_tvprovider = Tvprovider} The public Watchtvcontrol () {//injection is complete to use its Watchtv method _tvprovider.watcht   
        V (); }//Main program, code level implementation injection and call public partial class APP {private void Application_Startup (object sender, StartupEventArgs e) {Itvprovider Tvprovider   
               = new Konkatv ();   
               Watchtvcontrol Watchtvcontrol = new Watchtvcontrol ();   
        Watchtvcontrol.injecttv (Tvprovider); }     
}

With such a call, it is decoupled and. When you want to change the TV, you only need to replace in the main program, the rest of the functional modules do not need to change at all. (containers are injected into other TV entity classes.)

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.