Nopcommerce to achieve loosely coupled framework design purposes, the IOC framework was used:AUTOFAC. It has been tested that AUTOFAC is an IOC tool with good performance.
1. In the IOC, components need to be registered in the IOC first, with a configuration file. like Spring.net, there are also registered through the characteristics, such as structuremap, there are agents to register, like AUTOFAC. But IOC is a principle of separation of interfaces and implementations. All IOC is life. A specific class implements an interface. Then, when used, the system gets the implementation class of the interface from the IOC and creates the object.
2, below see how Nopcommerce uses AUTOFAC to implement loosely coupled framework design. in fact, its plug-in mechanism is also through the AUTOFAC to achieve.
The IOC's encapsulation and flexible use mechanism is mainly encapsulated in Nop.Core.Infrastructure. In Autofac, an object is also called a component. Component lifecycles are divided into single, temporary, and lifecycle domains and are defined as follows:
Namespace Nop.Core.Infrastructure.DependencyManagement
{public
enum Componentlifestyle
{
Singleton = 0,
Transient = 1,
lifetimescope = 2
}
The AUTOFAC has a container, provides method registration interfaces and their types, and also provides methods to find the type to register and automatically create objects.
3. Type Finder
To support plug-in functionality, as well as the ability to support some autoenrollment. The system provides a type finder. Itypefinder and implementation classes provide this functionality. You can find classes in this program domain by Type Finder, or you can find classes in all dynamic-link libraries throughout the Bin directory and register them in the Type reversal container. The Itypefinder and implementation classes are as follows:
4. Type Registration
Container Management class: Containermanager, manage the containers generated by AUTOFAC;
Container configurator: Containerconfigurer: Configuring dependency inversion container, establishing the relationship between type dependent registration and type lookup classes for the entire framework.
In the system there is a dependency class engine context: Enginecontext, which generates the engine based on the configuration file, which is responsible for returning objects from the container based on the type interface.
The system default engine nopengine, if no valid engine is configured, with the default engine, the generated engine is saved in a single instance 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), all reverse-dependent registration functions are implemented;
5. Container Registration Class
The system registers the interface is: Idependencyregistrar, the system registers this interface through Containerconfigurer and implements the class, And through the Itypefinder class search the assembly to implement the interface Idependencyregistrar class. The code is as follows:
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, Containermana Ger Containermanager, Eventbroker Broker, nopconfig configuration) {//other dependencies CONTAINERMANAGER.ADDC
Omponentinstance<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 the 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 (). By (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:
Namespace Nop.Core.Infrastructure.DependencyManagement
{public
interface Idependencyregistrar
{
<summary>
///This method registers dependencies 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 ordinal
///</summary>
int order {get;}
}
6. Single Case class container
The single class family holds a single instance object in the system that is the same lifecycle as the program, or it is called a single instance class container.
These include entity classes, collection classes, and a single example container for dictionary classes.
Singleton<t>,singletonlist<t>,singletondictionary<tkey, tvalue>. Enginecontext is the Singleton<t> class to manage the engine.
7, the MVC service provider class.
Type-dependent accessor: Nopdependencyresolver, by inheriting the interface under MVC: Idependencyresolver, and registering in the Application_Start method so that it is invoked at system startup.
Set Dependency resolver
var dependencyresolver = new Nopdependencyresolver ();
Dependencyresolver.setresolver (Dependencyresolver);
8. Other
Event interception class: Eventbroker: Filters requests sent to the system to prevent system crashes due to temporary errors or exceptions.
When the system starts, it performs tasks: Istartuptask, the task performed at startup is primarily the initialization and loading of the database.