How does the content part in Orchard work?

Source: Internet
Author: User
ArticleDirectory
    • Driver)
    • Handler)
    • How to work
    • Example
    • Summary
The article "Understanding Content Management in Orchard" describes how to organize content in Orchard. Here we talk about a very important thing -- Content Parts (Content Part ). Each part of the content is a complete small functional block, which implements UI rendering and data access of its own functions, which means it implements a complete encapsulation from the UI Layer to the database layer. How is this function implemented?

We know that there is a partialview concept in ASP. net mvc, which can implement a UI Layer encapsulation. If the data in this part needs to be stored separately, it still needs to be executed by the corresponding action. However, the content part of orchard may be called in other modules, and its data storage operations are also executed in the actions of other modules. How to effectively store and display the data of content components in the actions of other modules is the key to implementing the functions of content components. Two things are introduced in orchard to solve this problem --Drive(Driver) andProcessor(Handler ).

A driver is a class that is generally inherited from the contentpartdriver in the orchard framework. You can override the display method and edit the display method (executed during Editor-Get ), the editing interface submission method (executed during Editor-post), and other methods such as import and export. From the perspective of these rewriting methods, the drive is like the controller of the content part, and the method in the drive is like the action of the content part. The handler processor is also a class that generally inherits from the contenthandler in the orchard framework. It defines the behavior, events, and operational data models of the corresponding content components. We can understand it as the filter of the Content Part and tell the orchard framework how to process your part. How to work when a request needs to display a content. The system first finds the corresponding action in the Controller corresponding to the request according to the route rule. Then obtain the content in this action through the icontentmanager interface. When icontentmanager is used to obtain the content, the orchard framework automatically obtains the data of the relevant parts through the data storage filter provided by the relevant content component processor. Next, we will build a dynamic class model that needs to be displayed to the view through builddisplay. Each component of the composition content serves as the attribute of this dynamic class. Finally, the orchard view engine finds the corresponding display template based on the display method provided by each part drive, and finally combines it into a UI for the user. The principle of rendering and editing pages is roughly the same as that of submitting and Editing data. The work of the content component is as follows: it simply describes the execution process for obtaining the content component data. To use this function well, you need to carefully study it. Code Good experience (you can view the code about content display in the orchard core layer: \ orchard. web \ core \ contents \ controllers \ itemcontroller. CS ). In addition, the view engine functions of orchard also need to be well studied to better master the functions of the custom interface. Let's first understand it as a black box. As long as you give it a dynamic object, it will be able to find the rendering template based on the parts that constitute the dynamic object, and then combine and output the interface we want. Let's uncover the secret of this black box later. For example, if we need to implement a product content part, its function is to display some specific information of the product, such as price and brand. The drive code is as follows:

 

Productpartdriver. CS Using System;
Using System. LINQ;
Using Jetbrains. Annotations;
Using Orchard. contentmanagement;
Using Orchard. contentmanagement. drivers;
Using Orchard. contentmanagement. handlers;
Using Orchard. Security;
Using Mycompany. Products. models;

Namespace Orchard. Products. Drivers
{
///   <Summary>
/// The driver is equivalent to the Controller of the content part.
///   </Summary>
Public   Class Productpartdriver: contentpartdriver < Productpart >  
{
///   <Summary>
/// Execute when displaying the interface (equivalent to action)
///   </Summary>
///   <Param name = "part"> Equivalent to the model of this Part </Param>
///   <Param name = "displaytype"> Display type (for example, "details" (details) or summary (Summary )") </Param>
///   <Param name = "shapehelper"> Similar to the view engine, you can find the corresponding display template (equivalent to view) based on the displayed dynamic objects) </Param>
///   <Returns> This is equivalent to returning an actionresult. The orchard framework will process the returned value accordingly. </Returns>
Protected   Override Driverresult display (productpart part, String Displaytype, dynamic shapehelper)
{
Return Contentshape ( " Parts_product " ,() => Shapehelper. parts_product (
Price: part. Price,
Brand: part. Brand ));
}

///   <Summary>
/// Run the GET command when editing the page)
///   </Summary>
///   <Param name = "part"> </param>
///   <Param name = "shapehelper"> </param>
///   <Returns> </returns>
Protected   Override Driverresult Editor (productpart part, dynamic shapehelper)
{
Return Contentshape ( " Parts_product_edit " ,
() => Shapehelper. editortemplate (
Templatename: " Parts/product " ,
Model: part,
Prefix: prefix ));
}

///   <Summary>
/// Post)
///   </Summary>
///   <Param name = "part"> </param>
///   <Param name = "Updater"> </param>
///   <Param name = "shapehelper"> </param>
///   <Returns> </returns>
Protected   Override Driverresult Editor (productpart part, iupdatemodel Updater, dynamic shapehelper)
{
Updater. tryupdatemodel (part, prefix, Null , Null );
Return Editor (part, shapehelper );
}
}

 

The processor code is as follows:

 

Producthandler. CS Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using Orchard. contentmanagement. handlers;
Using Mycompany. Products. models;
Using Orchard. Data;

Namespace Mycompany. Products. handlers
{
///   <Summary>
/// Filter of processor equivalent content components
/// Currently, only the storage of content components of this product is defined.
///   </Summary>
Public   Class Producthandler: contenthandler
{
Public Producthandler (irepository < Productrecord > Repository)
{
// Data storage that defines productpart is processed through irepository <productrecord>.
// Irepository is interpreted as a data access layer class.
Filters. Add (storagefilter. For (repository ));
}
}

 

After summing up this article, we learned two concepts in orchard: drivers and processors, and how they work and how to implement content component functions. This part of content is the most distinctive part of orchard. to understand and master this part of content, you can start to develop a module that truly has the characteristics of orchard.

 

Reference:

Write a content part: Parts
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.