Create a product module with orchard features

Source: Internet
Author: User
ArticleDirectory
    • Requirement Analysis
    • Create a module code template
    • Create model
    • Create part
    • Create handler and driver
    • Create a placement.info File
    • Create part Template
    • Define product type
    • Create a background management Screen
    • Create a foreground display
    • Results
    • Summary
    • References
    • Download this example

This article explains the knowledge of orchard module development by creating a product module. For some content in this article, see the introduction in the official website document creating a module using a text editor.

Requirement Analysis

First, let's clarify what needs to be achieved in this example: create a product module in orchard to implement product management and display functions. The product must include the following fields: title, price, brand, and description. Based on the concept of content components introduced in previous articles, we can create a product component and combine several other existing components to form a new product type to implement product functions.

Create a module Code Template

In step 2, we also use the command line tool we have previously introduced to create a module code template.

Codegen module mycompany. Products

Create model

In step 2, we need to create a data model for product components to store product-specific data. In this example, we only need two fields: price and brand. In the models directory under the Module Directory, add a productrecord. CS file and enter the following code:

Productrecord. CS

 Using  System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using Orchard. contentmanagement. records;
Using Orchard. Data. Conventions;

Namespace Mycompany. Products. Models
{
Public Class Productrecord: contentpartrecord
{
Public Virtual Double Price { Get ; Set ;}
Public Virtual String Brand { Get ; Set ;}
}
}

To implement the content component function in orchard, the corresponding data model must inherit from the contentpartrecord class, so that the orchard framework can organize the relevant content according to the content ID. In addition, all attributes in the data model must be added with the virtual keyword. This should be the need for data access and or ing.

Create part

In step 2, we need to create the part of this product. If the data entity of the model data is used, the part can be understood as the business entity. Add a productpart. CS file in the models directory under the Module Directory and enter the following code:

Productpart. CS

 Using  System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using Orchard. contentmanagement;
Using System. componentmodel. dataannotations;
Using Orchard. Core. routable. models;

Namespace Mycompany. Products. Models
{
Public Class Productpart: contentpart < Productrecord >
{
[Required]
Public Double Price
{
Get { Return Record. Price ;}
Set {Record. Price = Value ;}
}

[Required]
Public String Brand
{
Get { Return Record. Brand ;}
Set {Record. Brand = Value ;}
}
}
}

To implement the content part function, the corresponding part class also needs to inherit from contentpart <productrecord>. Here, contentpart specifies the generic parameter productrecord. It indicates that the data of this productpart is from productrecord. At the same time, we can also add some verification on the UI Layer to this Part class, such as the price and brand attributes are required.

Create a database installation file

In step 2, we need to create a table that saves product data in the database. As mentioned in the previous section, creating a data table in orchard is not implemented by directly executing SQL statements. Instead, create a Migrations. CS file in the module and use code to define its data structure. We can still use the command line tool to generate the migrations file template. Enter the following command:

Codegen datamigration mycompany. Products

The modification code is as follows:

Migrations. CS

 Using  System;
Using System. Collections. Generic;
Using System. Data;
Using Orchard. contentmanagement. drivers;
Using Orchard. contentmanagement. metadata;
Using Orchard. contentmanagement. Metadata. builders;
Using Orchard. Core. Contents. extensions;
Using Orchard. Data. Migration;
Using Mycompany. Products. models;

Namespace Mycompany. Products
{
Public Class Migrations: datamigrationimpl
{
Public Int Create ()
{
// Create a product-specific property data table productrecord
Schemabuilder. createtable ( " Productrecord " , Table => Table
. Contentpartrecord () // Product ID. This field is actually associated with the system's core content table.
. Column < Double > ( " Price " ) // Product Price
. Column < String > ( " Brand " , Column => Column. withlength ( 200 )) // Product Brand
);

// Define productpart
Contentdefinitionmanager. alterpartdefinition ( Typeof (Productpart). Name, cfg => Cfg. attachable ());

// Define producttype
Contentdefinitionmanager. altertypedefinition ( " Product " ,
Cfg => Cfg. withpart ( " Productpart " ) // Including Product components
. Withpart ( " Routepart " ) // Routing Components
. Withpart ( " Bodypart " ) // Text parts
);

Return 1 ;
}
}
}

For more information about the migrations file, click here.

Create handler and driver

In step 2, we need to create the processor and drive for this product part to describe how this product part is displayed and how data is operated. In the article "How Content Parts in Orchard work", I have described the functions of handler and driver. The sample code can be directly used in this module, I will not go into details here.

Create a placement.info File

In step 2, we need to define the display position and display mode of this product part in the content. The definition file is in the root directory of the module. For more information, see the document understanding the placement.info file on the official website. In the future, I will study this document in depth. Here, we will first follow the descriptions in the examples on the official website, create this file in the module root directory and enter the following code:

Placement.info

<Placement>
<PlaceParts_product= "Content: 10"/>
<PlaceParts_product_edit= "Content: 7.5"/>
</Placement>
Create part Template

In step 2, we need to create something on the UI Layer of this module. If the driver of the content part is equivalent to the Controller in MVC, the template of the content part is equivalent to the view of the content part. According to the definition in the driver in this example, we need to create two templates, one for display screen and the other for editing screen. We can search for template rules in Orchard and create a parts directory and an editortemplates directory under the view directory of the module. Create a product. cshtml file in the parts directory to display the screen. Here we only need to display the two attributes of the two products. Enter the following code:

Product. cshtml

 
@ Using mycompany. Products. extensions;
@ Using mycompany. Products. models;
@ Using orchard. contentmanagement;
@ Using orchard. Utility. extensions;

Price:
@ Model. Price
<BR/>
Brand:
@ Model. Brand

Then, create a parts directory under the editortemplates directory and create a product. cshtml file for editing the screen. Enter the following code:

Product. cshtml

@ Model mycompany. Products. Models. productpart
< Fieldset >
@ Html. labelfor (M => M. Price, T ("price "))
@ Html. textboxfor (M => M. Price, new {@ class = "textmedium "})
@ Html. validationmessagefor (M => M. Price ,"*")
</ Fieldset >
< Fieldset >
@ Html. labelfor (M => M. Brand, T ("brand "))
@ Html. textboxfor (M => M. Brand, new {@ class = "textmedium "})
@ Html. validationmessagefor (M => M. brand ,"*")
</ Fieldset >
Define product type

The official website example ends here. Because a product part has been created, we only need to create a product type in type management in the orchard background, then find the product type we have defined in content management to add and manage the product. In this case, we only define a product component, not a product module in the real sense. The display interface and management interface of this product are implemented only by using the orchard content management function. To make the product module look more like a module, we still need to do the following.

Create a background management Screen

This step has already been pushed in our previous learning process. As described in "upgrade helloworld module in Orchard", we need to create a background management menu file adminmenu first. CS. Then, we need to add an admincontroller to perform operations on the background interface. Finally, we need to define the background interface view. This part of the code is large and can be downloaded later in this article.Source codeView.

Create a foreground display

We have also pushed this step before, as described in the article "Create a helloworld module in Orchard". To implement a foreground display screen, I need to define routes, implement the controller of the corresponding screen and create the view corresponding to the screen. The view display content involves the view engine mechanism in orchard, and the detailed principle has to be further studied. Here we first imitate the methods in other orchard modules to implement this part of content, you can also download the source code in this example.

Results

Log on to the background and enable this module. We can see the corresponding product management items in the background management menu.

Enter http: // orchard website domain name/products at the front-end to view the products added in the background.

Summary

This article describes the development process of a product module and describes the basic steps required for the development of an orcahrd module. Through this example, we can further consolidate some previously learned orchard knowledge, such as Part, handler, and driver. It also provides a prototype basis for us to further study orchard module development in the future. For example, in the next step, we can learn about widgets in Orchard and add widgets of the latest released product to the homepage.

References

Orchard official website examples: http://www.orchardproject.net/docs/Creating-a-module-with-a-simple-text-editor.ashx

Download this example

Click here

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.