Spring.net (1) Concept-control inversion (aka Dependency injection)

Source: Internet
Author: User

Spring. NET serves as an application framework for building enterprise-level. NET applications with many flexible and rich features such as dependency injection, AOP, data access abstraction, and ASP.

Control inversion:

Inversion of Control: The IOC: A design principle in object-oriented programming that can be used to reduce the degree of coupling between computer code. The most common of these is called Dependency injection (Dependency injection, or DI), and there is a way to call it "dependency lookup" (Dependency lookup). By controlling the inversion, the object is created by an external entity that regulates all objects within the system, passing a reference to the object on which it depends. It can also be said that the dependency is injected into the object.

http://q.cnblogs.com/q/33437/explanation

For example, there is a class ClassA in component A, and in component B there is an interface IB and its corresponding implementation class B1 and B2.

Well, now ClassA need to use the IB interface to do something, such as:

Public class ClassA {
public void DoSomething () {
IB B =???
B.dowork ();
}
}

Now the question is coming, IB B =??? Of these three??? What code do you want to write? Do you want to write IB B = new B1 (), or do you want to write IB B = new B2 ()?

Either way, ClassA relies heavily on IB implementations.

In this scenario, ClassA uses the new one B1 or B2 to achieve a dependency on IB, in other words, ClassA is actively gaining dependency.

Such a design would make classa difficult to expand, and we need to improve the design: using Dependency injection. As mentioned above, the problem is new here, that is, the dependence is class to take the initiative, then we need to solve this problem: Do not take the initiative to obtain the IB's dependence (through new), and let this dependence from the outside of ClassA "inject" into. There are several ways to inject, and the more common one is through the constructor, so we're going to change the ClassA to:

Public class ClassA {
private IB B;

Public ClassA (IB b) {
this.b = b;
}

Public dosomething () {
this.b.dowork ();
}
}

As you can see, ClassA does not rely on the implementation of IB when it comes in from the "note" in the constructor of the IB dependency. It can also be found that, in this refactoring process, we are "ClassA active acquisition of IB's dependence" into "the dependence of the IB from the outside into the ClassA", the direction of dependence reversed, so the dependency injection is also known as "inversion of control."

Another kind

Reference http://www.cnblogs.com/yahue/p/3508501.html

There is a pet:

Interface Pet    {        setvoid bark ();}   

Puppy:

Class Dog:pet    {        setvoid bark () {Console.WriteLine (" wang Bark and bark ... "); } }

People:

Class person    {        setset;}}   

a simple spring framework:

Project reference: Spring.core-the foundation of the entire framework, enabling the functionality of dependency Injection

spring.aop--provides aspect-oriented programming (AOP) support

SPRING.DATA--A defines an abstract data access layer that allows data access across a variety of data access technologies, from ADO to various ORM.

Project configuration file: App. Config

  

<?XML version= "1.0" encoding= "Utf-8"?><Configuration><configsections><SectiongroupName= "Spring"><SectionName= "Context"Type= "Spring.Context.Support.ContextHandler, Spring.core"/><SectionName= "Objects"Type= "Spring.Context.Support.DefaultSectionHandler, Spring.core"/></Sectiongroup></configsections><Spring><Context><Resourceuri= "File://objects.xml" ></resource > </context> <objects xmlns< Span style= "color: #0000ff;" >= "Http://www.springframework.net" > </objects> </></ Configuration>          

The Objects.xml property is always copied, otherwise the <resource uri= "File://objects.xml" configured above ></resource> not found.

<?XML version= "1.0" encoding= "Utf-8"?><Objectsxmlns= "Http://www.springframework.net"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xsi:schemalocation= "Http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"><ObjectId= "Person"Type= "Springdemo.person,springdemo"Singleton= "true"><PropertyName= "Pet"Ref= "Dog"></Property><PropertyName= "Name"Value= "Yahue"></Property></Object><Objectid= "Dog"  Type= "Springdemo.dog,springdemo"  Singleton= "true" > <property = "name"  Value= "Wang Choi "></property > </object>< Span style= "color: #0000ff;" ></objects>      

In the console program:

void Fistioc () {person p = ctx. GetObject ("Person as Person" ; Console.WriteLine (P.pet.name); Console.WriteLine ("-------------------------");}        

Call:

static Iapplicationcontext CTX = contextregistry.getcontext ();        void Main (string    

Console output:

Wong Choy

Spring.net (1) Concept-control inversion (aka Dependency injection)

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.