Let's talk about the evolution of IOC and the path of ioc.

Source: Internet
Author: User

Let's talk about the evolution of IOC and the path of ioc.

Preface

In the previous article, I mentioned Spring in the permission design implementation. net ioc framework, many Yuan users consulted me about the concept of IOC through the QQ group. I feel it is necessary to write a blog post to briefly introduce the evolution of IOC, and how to use third-party mature IOC containers in actual projects. We hope that they can be used to help you learn and understand IOC more intuitively.

Simple Layer 3

Friends who have worked on the OA system will face the following problem: OA process reminder. In the initial stage of the system design, the internal private message is used as a reminder. We usually define such a structure to complete this function.

Program dependency Diagram

 

The core code of private messages in the website is as follows:

1 /// <summary> 2 // private message in the site 3 /// </summary> 4 public class PrivilegeMessageBll 5 {6 /// <summary> 7 /// send Private message 8 // </summary> 9 // <param name = "message"> </param> 10 public void SendMessage (string message) 11 {12 // some business processing 13} 14}

 

The core code of Program Calling is as follows:

1 PrivilegeMessageBll privilegeMessageBll = new PrivilegeMessageBll (); 2 string message = "you have a new process"; 3 privilegeMessageBll. SendMessage (message );

When your program is complete and you are complacent, the leaders are here. Xiao Li, the human resources department has just called to support text message reminders. With a tangle of emotions, Xiao Li was thinking about how to modify the program code to minimize the impact of changes. Finally, Xiao Li decided to modify the program according to the following solution.

Interface Separation

 

So after several hours of struggle, the program code was changed to the following:

Core code of the message sending interface layer:

1 /// <summary> 2 /// message sending interface 3 /// </summary> 4 public interface ISendMessageBll 5 {6 /// <summary> 7 // send message interface 8 // </summary> 9 // <param name = "message"> </param> 10 void SendMessage (string message ); 11}

Core code of private messages on the site:

1 /// <summary> 2 // private message in the site 3 /// </summary> 4 public class PrivilegeMessageBll: ISendMessageBll 5 {6 /// <summary> 7 /// send private message 8 /// </summary> 9 /// <param name = "message"> </param> 10 public void SendMessage (string message) 11 {12 // some business processing 13} 14}

Core code for sending text messages:

1 /// <summary> 2 /// send SMS 3 /// </summary> 4 public class SMSBll: ISendMessageBll 5 {6 /// <summary> 7 // send SMS 8 /// </summary> 9 /// <param name = "message"> </param> 10 public void SendMessage (string message) 11 {12 // some business processing 13} 14}

The core code of message sending is as follows:

/// <Summary> /// message sending /// </summary> public class SendMessageBll {ISendMessageBll is%l; // The interface as a parameter shields the subclass differences, this is the line replacement principle in object orientation. // whether you pass in a private message on the Site or send a public SendMessageBll (ISendMessageBll paraBll) {ismBll = paraBll ;} /// <summary> /// send a message // </summary> /// <param name = "message"> </param> public void SendMessage (string message) {isw.l. sendMessage (message );}}

Main program call:

1 ISendMessageBll is%l = new PrivilegeMessageBll (); 2 SendMessageBll smBLL = new SendMessageBll (is%l); 3 string message = "you have a new process"; 4 smBLL. SendMessage (message );

Here, Xiao Li uses the interface as a parameter to direct the interface to the subclass, shielding the differences between the subclass. It can be understood as object-oriented polymorphism, which is not difficult to understand.

Another object-oriented principle is involved here: Dependency inversion.

What is dependency inversion? Dependency inversion can be simply understood as an interface defined by a high-level module, which is responsible for implementation.

In turn, the dependency is not inverted, which can be simply understood as: the lower-layer module defines the interface, and the higher-level module is responsible for implementation.

Here we use the Dependency inversion principle. The high-level module defines the interface (ISendMessageBll), and the lower layer module is responsible for implementation (PrivilegeMessageBll and SMSBll ).

By defining an interface constraint subclass, we can minimize the impact of new reminder methods on program code in the future. After the program is completed, the OA process is reminded to be stable for a long time.

After a long time, HR calls again. Xiao Li, the process reminder also needs to be followed by an RTX message reminder, so that employees can receive messages more quickly and intuitively. This is what Xiao Li found: every time a new reminder method is added, We need to instantiate a new object. This is so painful. The more I see, the less pleasing to the eye, because new reminders may be added in the future, this future uncertainty will continuously generate instances.

1 // Private Message reminder method 2 ISendMessageBll privilegeMessageBll = new PrivilegeMessageBll (); 3 SendMessageBll smBLL = new SendMessageBll (privilegeMessageBll ); 4 string message = "your new arrival process"; 5 // SMS reminder Method 6 ISendMessageBll smsBll = new SMSBll (); 7 SendMessageBll smBLL = new SendMessageBll (smsBll ); 8 string message = "your new arrival process"; 9 // RTX reminder mode 10 ISendMessageBll rtxBll = new RTXBll (); 11 SendMessageBll smBLL = new SendMessageBll (rtxBll ); 12 string message = "you have a new arrival process ";

At this time, Xiao Li came to the conclusion: how to control the number of times of instantiating ISendMessageBll? It is best to instantiate it only once.

After in-depth analysis, the key issue is how to obtain an ISendMessageBll instance. After constantly querying information, Xiao Li learned that there are three ways to obtain ISendMessageBll instances.

 

Simple Factory

The role of the factory model is to elegantly solve the Infinite new () operations when applications use objects, while reducing the coupling between system applications, this improves the maintainability and adaptability of the system. It is to encapsulate the changes generated by different reminder methods in an independent factory class and name it SendMessageFactory class. In this way, even if there are further changes, you only need to change some code in this class, it does not affect all other places in the program that use ISendMessageBll. Blog Park on the introduction of simple factory too much, here we recommend an article: http://www.cnblogs.com/hegezhou_hot/archive/2010/11/30/1892227.html

Abstract Factory

Abstract Factory relies on the node information of the configuration file in the factory mode, and then uses reflection to dynamically create corresponding instances. The advantage is that when the demand changes, we only need to modify the configuration file node, without modifying the code and re-compiling the program. For example, all process reminders are sent via intra-site private messages, so we only need to modify the corresponding XML value. Blog park on abstract factory introduction is also very much, I will not re-write, here also recommended an article: http://www.cnblogs.com/hegezhou_hot/archive/2010/12/01/1893388.html

IOC container

Here we will focus on the IOC container and use Spring. NET to implement dependency injection.

First, we need to understand the following concepts:

Control inversion (IOC): This is to transfer the creation and binding of dependent objects to the external part of the dependent object class.

Dependency injection (DI): it provides a mechanism to pass reference of objects that require dependency (low-level module) to objects that are dependent on (high-level module. Common methods include constructor injection and property injection.

In fact, in the above example, we have come into contact with the dependency injection (DI) and control inversion (IOC) concepts. Please refer to the following example.

1 /// <summary> 2 // message sending constructor injection method 3 /// </summary> 4 public class SendMessageBll 5 {6 ISendMessageBll is%l; 7 // pass DI, we can pass reference of any object in PrivilegeMessageBll and SMSMessageBll to SendMessageBll Class Object outside the SendMessageBll class. // The constructor is used to inject 9 public SendMessageBll (ISendMessageBll paraBll) 10 {11 isw.l = paraBll; 12} 13} 14 15 16 /// <summary> 17 // message sending attribute Injection Method 18 /// </summary> 19 public class SendMessageBll20 {21 // defines private variable save abstract 22 private ISendMessageBll _ isw.l; 23 // pass DI, we can pass any reference of any object in PrivilegeMessageBll and SMSMessageBll to the SendMessageBll Class Object outside the SendMessageBll class. // attribute injection 25 public ISendMessageBll is%l26 {27 set {_ ismBll = value ;} 28 get {return _ ishangl;} 29} 30 31 public SendMessageBll (ISendMessageBll paraBll) 32 {33 ishangl = paraBll; 34} 35}

Main program call method:

1 // constructor injection method call 2 ISendMessageBll ismBll = new PrivilegeMessageBll (); 3 SendMessageBll smBLL = new SendMessageBll (ismBll); 4 string message = "you have a new arrival process "; 5 smBLL. sendMessage (message); 6 7 // call 8 PrivilegeMessageBll privilegeMessageBll = new PrivilegeMessageBll (); 9 SendMessageBll smBLL = new SendMessageBll (); 10 smBLL. ismBll = privilegeMessageBll; 11 string message = "you have a new process"; 12 smBLL. sendMessage (message );

In fact, the above is the concept of dependency injection (DI). You should have a preliminary understanding.

Finally, I will briefly introduce how Spring. NET injects different databases in my previous article on permission design implementation.

Detailed Solution

 

1.Configure the Spring. NET node in WebConfig. For details, refer to the official help manual.

1 <! -- Configure spring --> 2 <sectionGroup name = "spring"> 3 <section name = "context" type = "Spring. context. support. contextHandler, Spring. core "/> 4 <section name =" objects "type =" Spring. context. support. defaultSectionHandler, Spring. core "/> 5 </sectionGroup> 6 7 <! -- Configure the spring path --> 8 <spring> 9 <context> 10 <resource uri = "~ /Config/Objects. xml "/> 11 </context> 12 </spring>

2.Create an Objects. xml file and configure nodes

1 <?xml version="1.0" encoding="utf-8" ?>2 <objects xmlns="http://www.springframework.net">3   <object id="BaseAccessDal" type="Murphy.Data.SQLServer.BaseAccessDal, Murphy.Data.SQLServer" />4 </objects>

X of type = "x, y" indicates the Assembly name, and y indicates the namespace of the Assembly.

3.Define BaseBll base class

1 /// <summary> 2 /// business logic layer base class 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 public abstract class BaseBll <T> where T: class 6 {7 /// <summary> 8 // the data interface layer object is waiting for instantiation 9 /// </summary> 10 protected IBaseDal <T> idal = null; 11}

4.BaseAccessBllInherit BaseBLL

1 /// <summary> 2 /// logon log 3 /// </summary> 4 public class BaseAccessBll: BaseBll <BaseAccess> 5 {6 protected IBaseAccessDal baseAccessDal = null; 7 // constructor injects 8 public BaseAccessBll () 9 {10 IApplicationContext springContext = ContextRegistry. getContext (); 11 baseAccessDal = springContext. getObject ("BaseAccessDal") as IBaseAccessDal; 12 base. idal = baseAccessDal; 13} 14}

Through the above four steps, we can easily inject different databases through Spring. NET dependency injection (DI). In fact, Spring. NET helps us to reflect those things.

If you are interested, help me in the lower right corner.[Recommended]Here, thank you. Next I will write an article based on the module list. Finally, I created a technical exchange group:263169088And you are welcome to discuss it.

Related Article

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.