Orchard document-understanding data access

Source: Internet
Author: User

Data access is different from traditional web applications in the orchard project, because data modeling uses code rather than database management systems. You can use code to define data attributes. The orchard framework creates a data component to save data.

If you need to change the data structure, you can write code to specify the changes. These changes will be transmitted to the database system through code. The Code-centric mode includes the abstraction layer, which allows you to reuse these components in different content types to add or change behavior without damaging other layers.

The key concepts of data access are as follows:

  • Records)
  • Data Migration)
  • Content handlers)
  • Content drivers)
Records)

A record is a class that represents the Content part data mode. Create a new record. You define a class that inherits from ContentPartRecord, and then add the attributes you need to save to the content component. Each attribute must be virtual. For example, a Map Part may contain the following records:

namespace Map.Models {    public class MapRecord : ContentPartRecord {        public virtual double Latitude { get; set; }        public virtual double Longitude { get; set; }    }}

Generally, Record class is placed in the Models folder. Its parent class,ContentPartRecordIt also contains an id attribute and a reference to the content item object. Therefore, instances of the MapRecord class not only includeLatitudeAndLongitudeAttributes, includingidAttribute and content item object, used to maintain the relationship between this component and other content.

When you define a content component, you use the record as follows:

namespace Map.Models {    public class MapPart : ContentPart<MapRecord> {        [Required]        public double Latitude        {            get { return Record.Latitude; }            set { Record.Latitude = value; }        }        [Required]        public double Longitude        {            get { return Record.Longitude; }            set { Record.Longitude = value; }        }    }}

 

Note that only data related to MapPart is defined. You do not need to define any attributes that need to maintain the relationship between MapPart and other content.

For a complete example of MapPart, see Writing a Content Part.

Data Migration)

Creating a record class does not create a data table. It only creates a data model. To create a data table, you must write a data migration class.

The data migration class allows you to create and update data table models. The code in the data migration class is executed when the administrator chooses to enable or update this component. The update method provides a historical record for Data Model update. When an update is available, the site administrator can choose to run the update.

You can create a data migration class by running the following orchard command line:

codegen datamigration <feature_name>

This command will createMigrations. csFile.CreateThe method is automatically added to the migration class.CreateMethod, you useSchemaBuilderClass to create a data table, suchMapPartFeature.

namespace Map.DataMigrations {    public class Migrations : DataMigrationImpl {        public int Create() {            // Creating table MapRecord            SchemaBuilder.CreateTable("MapRecord", table => table                .ContentPartRecord()                .Column("Latitude", DbType.Double)                .Column("Longitude", DbType.Double)            );            ContentDefinitionManager.AlterPartDefinition(                typeof(MapPart).Name, cfg => cfg.Attachable());            return 1;        }    }}

When defining data model attributes.ContentPartRecord(), You ensure that other important attributes are included in this table. In this example, the id attribute andLatitudeAndLongitudeAre included in this table.

The number returned is very important because it specifies the feature version number. You will use the version number to update the model.

You can addUpdateFromN method to update a data table.NIs the version number to be updated. The following code shows an update method for adding a new column in the data migration class:

namespace Map.DataMigrations {    public class Migrations : DataMigrationImpl {        public int Create() {            // Creating table MapRecord            SchemaBuilder.CreateTable("MapRecord", table => table                .ContentPartRecord()                .Column("Latitude", DbType.Double)                .Column("Longitude", DbType.Double)            );            ContentDefinitionManager.AlterPartDefinition(                typeof(MapPart).Name, cfg => cfg.Attachable());            return 1;        }        public int UpdateFrom1() {            SchemaBuilder.AlterTable("MapRecord", table => table                .AddColumn("Description", DbType.String)           );            return 2;        }    }}

The update method returns 2 because the version number is 2 after the new column is added. If you want to add a new update method, the update method should be namedUpdateFrom2().

When you add an update method to run the project, the module automatically updates it quietly.

Content Handlers)

The content processor is similar to the ASP. net mvc filter. In the processor, you define behavior for special events. In a simple content processor, you only define the record repository of the content component. The example is as follows:

namespace Map.Handlers {    public class MapHandler : ContentHandler {        public MapHandler(IRepository<MapRecord> repository) {            Filters.Add(StorageFilter.For(repository));        }    }}

In more advanced Content processors, you define event behaviors similar to feature publishing or activation. For more information about Content processors, see Understanding Content Handlers.

Content Drivers)

The content drive is similar to the controller of ASP. net mvc. It contains the code of the specified content component. It usually involves creating data shape content under different conditions, such as the display or editing mode. Typically, you loadDisplayAndEditorMethod, returnContentShapeResultObject.

For example, see Accessing and Rendering Shapes.

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.