Nopcommerce for the purpose of loosely coupled framework design, the IOC framework is used: AUTOFAC. According to some tests, AUTOFAC is an IOC tool with good performance.
1, in the IOC, the components first need to register in the IOC, there are registered through the configuration file, like Spring.net, but also through the characteristics of registration, like StructureMap, but also through the agent to register, like AUTOFAC. But the IOC pays attention to one principle, namely interface and realization separation. All IOC is life a specific class implements an interface. Then, when used, the system obtains the implementation class of the interface from the IOC and creates the object.
2. Nopcommerce How to use AUTOFAC to achieve a loosely coupled frame design. In fact, its plug-in mechanism is also through the AUTOFAC to achieve.
The IOC package and flexible use mechanisms are mainly encapsulated in the Nop.Core.Infrastructure. In Autofac, objects are also called components. The component life cycle is divided into: singleton, temporal, and life cycle domains, defined as follows:
[HTML]View PlainCopy
- Namespace Nop.Core.Infrastructure.DependencyManagement
- {
- public enum Componentlifestyle
- {
- Singleton = 0,
- Transient = 1,
- Lifetimescope = 2
- }
- }
AUTOFAC has containers, and provides methods for registering interfaces and their types, and also provides methods to find the types of registrations and automatically create objects.
3. Type Finder
In order to support plug-in features, as well as support some auto-registration features. The system provides a type finder. Itypefinder and implementation classes provide this functionality. The Type Finder lets you find classes in this program domain, or you can find classes in all dynamic-link libraries in the entire bin directory, and register them in a type reversal container. The Itypefinder and implementation classes are as follows:
4. Type Registration
Container Management classes: Containermanager, managing containers generated through AUTOFAC;
Container configurator: Containerconfigurer: Configures a dependency reversal container that establishes the relationship between the type-dependent registration of the entire framework and the type lookup class.
In the system there is a dependency on the class engine context: Enginecontext, which can be generated from the configuration file engine, which is responsible for returning objects from the container based on the type interface.
The system default engine Nopengine, if not configured with a valid engine, the default engine, the generated engine is saved in a singleton container.
Their relationship is as follows:
The system initializes the engine context in the method Application_Start of the class mvcapplication. and by calling Enginecontext.initialize (false) to implement all the inversion-dependent registration functions;
5. Container Registration Class
The system registers the interface as: Idependencyregistrar, the system registers this interface through the Containerconfigurer and implements the class, And the class that implements the interface Idependencyregistrar through the Itypefinder class search assembly. The code is as follows:
[CSharp]View PlainCopy
- Namespace Nop.Core.Infrastructure.DependencyManagement
- {
- // <summary>
- // configures the inversion of control container with services used by NOP.
- // </summary>
- public class Containerconfigurer
- {
- public virtual void Configure (Iengine engine, Containermanager Containermanager, Eventbroker broker, Nopconfig configuration)
- {
- //other Dependencies
- Containermanager.addcomponentinstance<nopconfig> (Configuration, "Nop.configuration");
- Containermanager.addcomponentinstance<iengine> (engine, "Nop.engine");
- Containermanager.addcomponentinstance<containerconfigurer> (This, "Nop.containerconfigurer");
- //type Finder
- Containermanager.addcomponent<itypefinder, webapptypefinder> ("Nop.typefinder");
- //register Dependencies provided by other assemblies
- var typeFinder = containermanager.resolve<itypefinder> ();
- Containermanager.updatecontainer (x =
- {
- var drtypes = typefinder.findclassesoftype<idependencyregistrar> ();
- var drinstances = new list<idependencyregistrar> ();
- foreach (Var drtype in drtypes)
- Drinstances.add ((Idependencyregistrar) activator.createinstance (Drtype));
- //sort
- Drinstances = Drinstances.asqueryable (). (t = t.order). ToList ();
- foreach (Var dependencyregistrar in drinstances)
- Dependencyregistrar.register (x, TypeFinder);
- });
- //event Broker
- Containermanager.addcomponentinstance (broker);
- }
- }
- }
The contents of the interface Idependencyregistrar are as follows:
[CSharp]View PlainCopy
- Namespace Nop.Core.Infrastructure.DependencyManagement
- {
- public interface Idependencyregistrar
- {
- // <summary>
- /// This method registers the dependency relationship through Containerbuilder.
- // </summary>
- /// <param name= "Builder" > Container manager Class </param>
- /// <param name= "TypeFinder" > Type Finder interface </param>
- void Register (Containerbuilder builder, Itypefinder TypeFinder);
- // <summary>
- // Registration sort serial number
- // </summary>
- int Order { get;}
- }
- }
6. Single class container
The Singleton Class series holds the Singleton object in the system with the same life cycle as the program, or it is called a singleton class container.
This includes an entity class, a collection class, and a singleton container for a dictionary class.
Singleton<t>,singletonlist<t>,singletondictionary<tkey, tvalue>. Enginecontext is to manage the engine through the Singleton<t> class.
7, the MVC service provides the class.
Type dependent: Nopdependencyresolver, which inherits the interface under MVC: Idependencyresolver, and registers it in the Application_Start method, so that it is called at system startup.
[CSharp]View PlainCopy
- Set Dependency resolver
- var dependencyresolver = new Nopdependencyresolver ();
- Dependencyresolver.setresolver (Dependencyresolver);
8. Other
Event interception class: Eventbroker: Filters the requests sent to the system to prevent the system from crashing due to temporary errors or exceptions.
Perform tasks at system startup: Istartuptask, the task performed at startup is primarily the initialization and loading of the database.
One of the Nopcommerce schema analysis----dependent class generation container