Basic principle
Plug-in applications are generally defined plug-in interface, and then put the plugin compiled DLL into a fixed directory, the application main program by loading those implementation of the plug-in interface DLL to implement plug-in use. Nopcommerce is the same, but as an MVC WEB application there will be some difference, first of all the different levels of trust (full Trust,medium Trust) when the strategy to load the DLL will not be the same, but also how to display the View in the plugin problem.
Put the plugin folder
Nopcommerce plug-ins are placed in the Plugins directory of the home directory of the website, Plugins there are many folders, a plug-in class library is a folder. In the plug-in class library, modify the address of the compiled output to the Plugins folder of the Web site home directory, so that the DLL generated by the plugin can be automatically under the target folder. Such as:
The other folder is the Shadowcopy folder, just under the Plugins/bin folder. about why to use Shadowcopy, live This article is described, Nopcommerce is to refer to its implementation. The problem of trust level is also described in detail.
Website launch
Let's look at the Pluginmanager.initialize method first. By defining the following properties on the PlugInManager class, the Initialize method of PlugInManager is guaranteed to run at the beginning of the site, earlier than Application_Start.
[Assembly:preapplicationstartmethod (typeof (PlugInManager), "Initialize")]
Before Application_Start, the initialization code was run primarily so that the Web site application could refer to the loaded DLL.
Core class
IPluginFinder.cs interface: Gets the information interface of the plug-in, Nop.Web.Framework.DependencyRegistrar registers this interface in the IOC. The system is loaded into memory when it is started.
IPlugin.cs: Plug-in operation interface, mainly set the plugin's property information, install plug-in interface, uninstall plug-in interface.
BasePlugins.cs implements the IPlugin.cs method.
PluginDescriptor.cs plug-in entity class, contains the plug-in version, description, type, file name, author, and so on a series of States.
The PluginFileParser.cs contains the entity action method for the plug-in, mainly writing the plugin's descriptive information.
PluginFinder.cs loads all of the plugins and gets their information.
PluginManager.cs plug-in management of the main class, look inside the comments, its plug-in mechanism should be referred to Umbraco this CMS.
Loading plugins
Load the description of the plugin first. Each plug-in must define a plug-in description file, the text file Description.txt to define, the name is also agreed, not other names. Add Description.txt to the plugin's class library to describe the plugin. The format of text fields in Description is fixed, and PlugInManager converts Description.txt files into Plugindescriptor classes, which are then stored in memory. The Displayorder field in Description represents the order of the plug-in for display on the interface. Get all the plug-in description file, it will go to InstalledPlugins.txt inside see, in the InstalledPlugins.txt inside some is already installed check pieces, no word is not installed. The Plugindescriptor.installed property describes this information.
Copy the DLLs that need to be loaded into the Plugins/bin folder, of course, to inherit from the IPlugIn interface, by Assembly.Load at home this DLL, and then use buildmanager.addreferencedassembly to put this DLL is loaded into the Web site of this application. It is important to note that buildmanager.addreferencedassembly must be added to the application_prestartinit process of the early site program, that is, before Application_Start. This is the plugin to be referenced is placed under the Plugins/bin, and is referenced by the CLR. The referenced plug-in's Assembly reference will be saved in the static list of PlugInManager Referencedplugins. This concludes the Initialize method for PlugInManager.
Display plugins on the interface
For example, when defining a plugin, we define a type of plug-in, such as a shipping method. Then we will inherit 2 interfaces when we define the plug-in, one is the IPlugin interface, the other is the Ishippingmethod interface. Pass pluginfinder.getplugins<t> () when you need to display the delivery mode plugin. ToList () method to get. Pluginfinder will go to the last step of the Pluginmanager.referencedplugins list to find out. Returns an instance of Ishippingmethod. Nopcommerce has a TXT file: InstalledPlugs.txt. Only the plugins inside this will eventually load into the interface. You can add a plugin to this file that will eventually need to be used for your website via the Nopcommerce Search Management page.
Configure Plug-ins
The Nopcommerce admin site can be configured for plugins. For example, Display Order and IsActive can be configured. The main logic is to update the plugin's Description.txt file and the in-memory properties inside the Iplugin.descriptor.
Define the Controller,action and View in the plugin
A slightly more complex plugin basically contains the interface and logic that you want to handle. So the plugin's interface View and related Controller and Action can be defined in the plugin's class library. In the establishment of Controller and View, not necessarily according to the specification of the Controllers folder and Views folder to define, you can define your own style.
To return to the view in Action, enter the name of the view that contains the namespace, for example:
return View ("Nop.Plugin.Payments.CashOnDelivery.Views.PaymentCashOnDelivery.Configure", model);
Because, in the compiled plug-in DLL, the View as an embedded resource is compiled into a name The Nop.Plugin.Payments.CashOnDelivery.Views.PaymentCashOnDelivery.Configure.cshtml resource file.
Nopcommerce The configuration information of some plug-ins into the database through the Configure interface of some plugins. It is then retrieved from the database when the foreground page is displayed.
Read the embedded Resource View
The plug-in is loaded into the application domain as a class library. While defining the plugin's View, you need to modify the properties of the cshtml file to Embedded Resource. It is put into the AppDomain as an embedded resource. We can use VirtualPathProvider to enable WEB applications to retrieve resources from the virtual file system, which you can find in this article, and the Nopcommerce implementation is the same as this article. There is a embeddedviews folder in the Nop.Web.Framework class library in Nopcommerce that contains some classes that handle embedded View. Finally, you need to register in Global.asax. The code for Nopcommerce is as follows:
1 // Register virtual path provider for embedded views 2 var embeddedviewresolver = enginecontext.current.resolve<iembeddedviewresolver>(); 3 var New Embeddedviewvirtualpathprovider (Embeddedviewresolver.getembeddedviews ()); 4 Hostingenvironment.registervirtualpathprovider (Embeddedprovider);
Writing Nopcommerce Plugins
You can refer to the official documentation. One of the suggestions is very good, is Copy the original plug-in, the above modification.
The Nopcommerce related plug-in part of the code was extracted, can be downloaded from here.
[goto] Nopcommerce MVC plug-in mechanism analysis