Control reversal & dependency Injection

Source: Internet
Author: User

Inversion of control (IOC) is an important Object-Oriented Programming Law to reduce the coupling problem of computer programs. Another name is dependency injection ). DI for short.

IOC and Di are both design patterns. To understand them, you must first understand them. The general understanding can be: the IOC model is the sublimation of the factory model, and the IOC can be seen as a large factory, however, all the objects to be generated in this large factory are defined in the XML file, and then the corresponding objects are generated according to the class name given in XML using the "reflection" programming. From the implementation point of view, IOC is to generate code for the previously written object in the factory method, which is defined by the XML file, that is, to separate the factory and object generation, the purpose is to improve flexibility and maintainability.

The commonly used open-source architecture to achieve control reversal: structuremap, objectbuilder, unity, autofac... about their understanding and download can be obtained on the open-source website http://www.oschina.net.

Structuremap is the only framework I have used in projects. So the following is the related Code. For other methods, you can explore it on your own. Structuremap is a lightweight dependency injection tool in the. NET environment. structuremap is also a flexible and scalable General "plug-in" mechanism for. NET applications.

Procedure:

1. initialize the object container when the application is enabled. It is used to load all object mappings and object associations.

1  void Session_Start(object sender, EventArgs e)2         {3             BootStrap.Init();4         }

Global. CS

2. Code ing configuration can also be configured in the XML file. I will not discuss the usage methods here.

using System;using System.Linq;using FluentNHibernate;using FluentNHibernate.Cfg;using NHibernate;using StructureMap;namespace MedicalCare.IoCSetup{    public static class BootStrap    {        public static void Init()        {            ObjectFactory.Initialize(x =>                {                    x.For<IScheduleAdaptor>().Use<ScheduleAdaptor>();                    x.For<IUserInfoToUserMessage>().Use<UserInfoToUserMessage>();                    x.For<IFamilyListToScheduleList>().Use<FamilyListToScheduleList>();                    x.For<IUserLoginValidator>().Use<UserLoginValidator>();                               }            );        }

3. The instance for getting the object interface implementation in the class indicates that only the specific implementation class object is bound at runtime, and a decoupling operation is implemented.

Note: ischeduleadaptor and scheduleadaptor are an implementation of interfaces and interfaces respectively.

  private IList<ScheduleEntity> CheckSchedule(ScheduleMessage message)        {            var scheduleAdaptor = ObjectFactory.GetInstance<IScheduleAdaptor>();            return scheduleEntityList;        }

View code

 

 

 

 

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.