Nopcommerce Architecture Analysis (reprint)

Source: Internet
Author: User
Tags nopcommerce

Original

First, the beginning of Nopcommerce architecture analysis

Nopcommerce is a relatively mature business application framework in. NET open source projects and a model in e-commerce systems. So I want to learn more about the design and implementation of the way.

Second, Nopcommerce structure analysis of the reference

Reference:dependencyresolver

Http://www.cnblogs.com/RobbinHan/archive/2011/11/30/2269537.html

Simple use of Dependency injection framework AUTOFAC

Http://www.cnblogs.com/liping13599168/archive/2011/07/16/2108209.html


The core of the modelbinder--asp.net MVC model binding

Http://www.cnblogs.com/artech/archive/2012/05/21/model-binder-provision.html

How ASP. NET MVC works?

Http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html


View View new engine Razor

Http://www.cnblogs.com/sonykings/archive/2013/05/23/3095539.html

ASP. NET MVC 3: @model new keywords in razorhttp://blog.csdn.net/Sandy945/article/details/6046285

Third, one of the Nopcommerce architecture analysis----dependent class generation container

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
    1. Namespace Nop.Core.Infrastructure.DependencyManagement
    2. {
    3. public enum Componentlifestyle
    4. {
    5. Singleton = 0,
    6. Transient = 1,
    7. Lifetimescope = 2
    8. }
    9. }


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
  1. Namespace Nop.Core.Infrastructure.DependencyManagement
  2. {
  3. // <summary>
  4. // configures the inversion of control container with services used by NOP.
  5. // </summary>
  6. public class Containerconfigurer
  7. {
  8. public virtual void Configure (Iengine engine, Containermanager Containermanager, Eventbroker broker, Nopconfig configuration)
  9. {
  10. //other Dependencies
  11. Containermanager.addcomponentinstance<nopconfig> (Configuration, "Nop.configuration");
  12. Containermanager.addcomponentinstance<iengine> (engine, "Nop.engine");
  13. Containermanager.addcomponentinstance<containerconfigurer> (This, "Nop.containerconfigurer");
  14. //type Finder
  15. Containermanager.addcomponent<itypefinder, webapptypefinder> ("Nop.typefinder");
  16. //register Dependencies provided by other assemblies
  17. var typeFinder = containermanager.resolve<itypefinder> ();
  18. Containermanager.updatecontainer (x =
  19. {
  20. var drtypes = typefinder.findclassesoftype<idependencyregistrar> ();
  21. var drinstances = new list<idependencyregistrar> ();
  22. foreach (Var drtype in drtypes)
  23. Drinstances.add ((Idependencyregistrar) activator.createinstance (Drtype));
  24. //sort
  25. Drinstances = Drinstances.asqueryable (). (t = t.order). ToList ();
  26. foreach (Var dependencyregistrar in drinstances)
  27. Dependencyregistrar.register (x, TypeFinder);
  28. });
  29. //event Broker
  30. Containermanager.addcomponentinstance (broker);
  31. }
  32. }
  33. }


The contents of the interface Idependencyregistrar are as follows:

[CSharp]View Plaincopy
  1. Namespace Nop.Core.Infrastructure.DependencyManagement
  2. {
  3. public interface Idependencyregistrar
  4. {
  5. // <summary>
  6. /// This method registers the dependency relationship through Containerbuilder.
  7. // </summary>
  8. /// <param name= "Builder" > Container manager Class </param>
  9. /// <param name= "TypeFinder" > Type Finder interface </param>
  10. void Register (Containerbuilder builder, Itypefinder TypeFinder);
  11. // <summary>
  12. // Registration sort serial number
  13. // </summary>
  14. int Order { get;}
  15. }
  16. }


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
    1. Set Dependency resolver
    2. var dependencyresolver = new Nopdependencyresolver ();
    3. 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.

Nopcommerce Architecture Analysis (reprint)

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.