Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Autofac dependency injection Class Generation Container in NopCommerce

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of the Autofac dependency injection class generation container in NopCommerce, which has a certain reference value, and interested friends can refer to it. I hope you will learn a lot after reading this article.

In order to achieve the purpose of loosely coupled framework design, NopCommerce uses the IOC framework: Autofac. According to some tests, Autofac is a good IOC tool.

1. In IOC, components first need to be registered in IOC, some of which are registered through configuration files. Like Spring.net, some are registered through features, such as StructureMap, and some are registered through agents, like Autofac. But IOC pays attention to one principle, that is, the separation of interface and implementation. All IOC is life. A concrete class implements an interface. Then, when in use, the system gets the implementation class of the interface from IOC and creates the object.

2. Let's take a look at how NopCommerce uses Autofac to achieve loosely coupled framework design. In fact, its plug-in mechanism is also implemented through Autofac.

The encapsulation and flexible use mechanism of IOC are mainly encapsulated in Nop.Core.Infrastructure. In Autofac, objects are also called components. The component lifecycle is divided into singleton, temporary and lifecycle domains, which are defined as follows:

Namespace Nop.Core.Infrastructure.DependencyManagement {public enum ComponentLifeStyle {Singleton = 0, Transient = 1, LifetimeScope = 2}}

Autofac has containers and provides a method registration interface and its types, methods to find registered types, and automatic creation of objects.

3. Type Finder

In order to support plug-in functions, as well as support some auto-registration features. The system provides a type finder. ITypeFinder and implementation classes provide this functionality. With Type Finder, you can find classes in this program domain, or you can find all dynamic link libraries in the entire bin directory and register them in the type inversion container. The ITypeFinder and implementation classes are as follows:

4. Type registration

Container management class: ContainerManager, which manages containers generated through Autofac

Container Configurator: ContainerConfigurer: configuration dependency reverses the container, establishing the relationship between type dependency registration and type lookup classes for the entire framework.

There is a dependent class engine context in the system: EngineContext, which generates an engine based on the configuration file, which is responsible for returning objects from the container based on the type interface.

System default engine NopEngine, if there is no valid engine configured, that is, the default engine is used, and the generated engine is saved in a singleton container.

Their relationship is as follows:

The system initializes the engine context in the MvcApplication-like method Application_Start. And realize all the registration functions of reversing dependencies by calling EngineContext.Initialize (false)

5. Container registration class

The system registration interface is: IDependencyRegistrar, the system registers this interface and the implementation class through the ContainerConfigurer, and searches the class of the interface IDependencyRegistrar in the assembly through the ITypeFinder class. The code is as follows:

Namespace Nop.Core.Infrastructure.DependencyManagement {/ Configures the inversion of control container with services used by Nop. / / public class ContainerConfigurer {public virtual void Configure (IEngine engine, ContainerManager containerManager, EventBroker broker, NopConfig configuration) {/ / other dependencies containerManager.AddComponentInstance (configuration, "nop.configuration"); containerManager.AddComponentInstance (engine, "nop.engine"); containerManager.AddComponentInstance (this, "nop.containerConfigurer"); / / type finder containerManager.AddComponent ("nop.typeFinder"); / / register dependencies provided by other assemblies var typeFinder = containerManager.Resolve () ContainerManager.UpdateContainer (x = > {var drTypes = typeFinder.FindClassesOfType (); var drInstances = new List (); foreach (var drType in drTypes) drInstances.Add ((IDependencyRegistrar) Activator.CreateInstance (drType)); / / sort drInstances = drInstances.AsQueryable (). OrderBy (t = > t.Order). ToList (); foreach (var dependencyRegistrar in drInstances) dependencyRegistrar.Register (x, typeFinder);}); / / event broker containerManager.AddComponentInstance (broker) }}}

The content of the API IDependencyRegistrar is as follows:

Namespace Nop.Core.Infrastructure.DependencyManagement {public interface IDependencyRegistrar {/ this method registers dependencies through ContainerBuilder. / / Container Manager Class / / Type Finder Interface void Register (ContainerBuilder builder, ITypeFinder typeFinder); / Registration sequence number / int Order {get;}

6. Singleton container

The singleton class series stores singleton objects in the system that have the same life cycle as the program, or singleton class containers.

This includes singleton containers for entity classes, collection classes, and dictionary classes.

Singleton,SingletonList,SingletonDictionary . EngineContext manages the engine through the Singleton class.

7. MVC service provider class.

Type dependent acquirer: NopDependencyResolver, which is called at system startup by inheriting the interface under mvc: IDependencyResolver and registering it in the Application_Start method.

/ / set dependency resolver var dependencyResolver = new NopDependencyResolver (); DependencyResolver.SetResolver (dependencyResolver)

8. Other

Event blocking class: EventBroker: filters requests sent to the system to prevent the system from crashing due to temporary errors or exceptions.

When the system starts, it executes the task: IStartupTask, and the main task is initializing and loading the database.

Thank you for reading this article carefully. I hope the article "sample Analysis of Autofac dependency injection Class Generation Container in NopCommerce" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report