DDD-based. NET Development Framework-ABP Dependency Injection

Source: Internet
Author: User

Back to ABP Series

The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.

ASP. Boilerplate is a new starting point for developing modern web applications with best practices and popular technologies, and is designed to be a common Web application framework and project template.

ABP's official website: http://www.aspnetboilerplate.com

ABP Official Document: Http://www.aspnetboilerplate.com/Pages/Documents

Open source project on GitHub: Https://github.com/aspnetboilerplate

I. The concept of dependency injection

Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also the core of the lightweight spring framework. Control reversals are generally divided into two types, dependency injection (Dependency injection, short di), and dependent lookup (Dependency lookup). The application of dependency injection is quite extensive.

Dependency Injection is a software design pattern in which one or more dependency injections (or services), or passed by reference, are part of a dependent object (or client) and client state. The pattern establishes a client's dependency behavior, which allows the program design to be loosely coupled, relying on inversion and a single responsibility principle. It directly contrasts the service locator model, which allows customers to understand the systems they are using to find dependencies.

Dependency injection is not an end, it is a set of tools and means, and the ultimate goal is to help us develop loosely coupled, maintainable, testable code and programs. The practice of this principle is well-known interface-oriented, or abstract-oriented programming.

The ideal software development design is "high cohesion, low coupling", cohesion focus on object-oriented programming, low-coupling focus on interface programming, control inversion, dependency injection, dependency inversion contains the idea of interface-oriented programming.

Common Dependency Injection Frameworks:

Unity: The Microsoft Patterns&practicest Team developed an IOC dependency injection framework that supports AOP crosscutting concerns.

MEF (Managed Extensibility Framework): a framework for extending. NET applications that can develop plug-in systems.

Spring.net: Dependency Injection, aspect-oriented programming (AOP), data access abstraction, and ASP.

AUTOFAC: The most popular Dependency injection and IOC framework, lightweight and high-performance, with virtually no intrusion into project code.

Postsharp: Implement static AOP crosscutting concerns, simple and powerful, without any changes to the method of target interception.

Castle Windsor, StructureMap, Ninject

In fact, I feel autofac very useful, has been using the AUTOFAC, do not know how to Castle Windsor.

Layer two or three and ddd layered dependencies

1, three layers of layered dependence such as:

From the reference relationship we can know the dependencies of each layer: the BLL needs to rely on the DAL, because the DAL layer entities are used in the BLL. The UI layer relies on the BLL and relies on the DAL, because the DAL layer entity is also used in the UI.

If the DAL needs to be modified from a different database, the Dal dependency needs to be modified.

2, DDD hierarchical dependency diagram

It can be known that both the presentation layer and the data access layer depend on the domain model layer, so that if we add a new UI interface, and replace the storage and acquisition of a data source, only the code of the corresponding layer needs to be modified, the domain model layer remains stable.

The best way to reduce the dependency and tight coupling introduced by new is to use a constructor injection that relies on this design pattern: that is, if we need a dependent instance, it is injected through the constructor.

Decoupling and the most important principle is the dependency inversion principle:

High-level modules should not rely on the underlying modules, they should all rely on abstraction. Abstractions should not be dependent on detail, and detail should be dependent on abstraction.

The simple understanding is that components should depend on interfaces rather than implementations.

Iii. ABP Dependency Injection bottom-level implementation

ABP Dependency Injection is implemented through the framework of Castle Windsor Dependency injection.

1, through the implementation of the Iconventionaldependencyregistrar instance definition Injection convention, and then through the Iocmanager to read this rule completion Dependency injection

Code under the Dependency folder in the ABP project file

1) Add Basicconventionalregistrar to the list of Iocmanager Iconventionaldependencyregistrar in the Preinitialize method

Iocmanager.addconventionalregistrar (new Basicconventionalregistrar ());

2) Iocmanager maintains a list called _conventionalregistrars, where the element type is Iconventionaldependencyregistrar. Then Iocmanager's registerassemblybyconvention is called in the Initialize method of the module.

         Public Override void Initialize ()        {            iocmanager.registerassemblybyconvention (assembly.getexecutingassembly ());                    }

3) Iocmanager iterates through the list in the Registerassemblybyconvention method and completes the register according to the rules defined in the Iconventionaldependencyregistrar instance.

        /// <summary>        ///registers types of given assembly by all conventional registrars. See<see cref= "Addconventionalregistrar"/>method. /// </summary>        /// <param name= "assembly" >Assembly to register</param>         Public voidregisterassemblybyconvention (Assembly Assembly) {registerassemblybyconvention (Assembly,Newconventionalregistrationconfig ()); }        /// <summary>        ///registers types of given assembly by all conventional registrars. See<see cref= "Addconventionalregistrar"/>method. /// </summary>        /// <param name= "assembly" >Assembly to register</param>        /// <param name= "config" >Additional Configuration</param>         Public voidregisterassemblybyconvention (Assembly Assembly, conventionalregistrationconfig config) {varContext =NewConventionalregistrationcontext (Assembly, This, config); foreach(varRegistererinch_conventionalregistrars) {Registerer.            registerassembly (context); }            if(config.            Installinstallers) {Ioccontainer.install (fromassembly.instance (assembly)); }        }

2, directly using the Register method of Iocmanager directly to complete the injection

Abpmodule has a member of a protected Iocmanager, so Abpmodule's derived classes can use this Iocmanager to complete the registration.

 Public classAbpwebmodule:abpmodule {/// <inheritdoc/>         Public Override voidpreinitialize () {if(HttpContext.Current! =NULL) {xmllocalizationsource.rootdirectoryofapplication= HttpContext.Current.Server.MapPath ("~"); }            //Iocmanager Direct InjectionIocmanager.register<iabpwebmoduleconfiguration, abpwebmoduleconfiguration>(); CONFIGURATION.LOCALIZATION.SOURCES.ADD (NewDictionarybasedlocalizationsource (Abpweblocalizedmessages.sourcename,NewXmlembeddedfilelocalizationdictionaryprovider (assembly.getexecutingassembly (),"Abp.Web.Localization.AbpWebXmlSource"                        ))); }        /// <inheritdoc/>         Public Override voidInitialize () {iocmanager.registerassemblybyconvention (assembly.getexecutingassembly ()); }    }

DDD-based. NET Development Framework-ABP 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.