philzhou

导航

nopCommerce : Updating an existing entity. How to add a new property.

Updating an existing entity. How to add a new property.

This tutorial covers how to add a property to the Affiliate entity that ships with the nopCommerce source code.

The data model

Entities will have two classes that are used to map records to a table. The first class defines the properties, fields, and methods consumed by the web application.

File System Location: [Project Root]\Libraries\Nop.Core\Domain\Affiliates\Affiliate.cs
Assembly: Nop.Core
Solution Location: Nop.Core.Domain.Affiliates.Affilate.cs

The second class is used to map the properties defined in the class above to their respective SQL columns. The mapping class is also responsible for mapping relationships between different SQL tables.

File System Location: [Project Root]\Libraries\Nop.Data\Mapping\Affiliates\AffiliateMap.cs
Assembly: Nop.Data
Solution Location: Nop.Data.Mapping.Affiliates.AffiliateMap.cs

Add the following property to the Affiliate class.

// Instance members must be virtual on data table objects like Affiliate.cs
// Virtual is required by data access frameworks so that these frameworks
// can implement more complex features like lazy loading.
public virtual string AffiliateWebSite { get; set; }

Add the following code to the constructor of the AffiliateMap class.

// This code maps a column in the database to the new property we created above
// This creates a nullable nvarchar with a length of 255 characters 
// in the Affiliate SQL table
this.Property(m => m.AffiliateWebSite).HasMaxLength(255).IsOptional();	

Because I’m all about results, at this point I would run the code, re-install the database, and verify that the column was created appropriately.

The presentation model

The presentation model is used to transport information from a controller to the view (read more at asp.net/mvc). Models have another purpose; defining requirements.

We configured our database to only store 255 characters for the AffiliateWebSite. If we try and save an AffiliateWebSite with 300 characters the application will break (or truncate the text). We want the application to protect users from failures the best we can, and our view models help enforce requirements like string length.

File System Location: [Project Root]\Presentation\Nop.Web\Administration\Models\Affiliates\AffiliateModel.cs
Assembly: Nop.Admin
Solution Location: Nop.Admin.Models.Affiliates.AffiliateModel.cs

The validator class is used to validate the data stored inside of the model class (e.g. required fields, max length, and required ranges).

File System Location: [Project Root]\Presentation\Nop.Web\Administration\Validators\Affiliates\AffiliateValidator.cs
Assembly: Nop.Admin
Solution Location: Nop.Admin.Validators.Affiliates.AffiliateValidator.cs

Add the property to our view model.

// The NopResourceDisplayName provides the "key" used during localization
// Keep an eye out for more about localization in future blogs
[NopResourceDisplayName("Admin.Affiliates.Fields.AffiliateWebSite")]
public string AffiliateWebSite { get; set; }

The requirements code will be added in the constructor of the validator.

//I think this code can speak for itself
RuleFor(m => m.AffiliateWebSite).Length(0, 255);
The view

File System Location: [Project Root]\Presentation\Nop.Web\Administration\Views\Affiliates\ _CreateOrUpdate.cshtml
Assembly: Nop.Admin
Solution Location: Nop.Admin.Views.Affiliates._CreateOrUpdate.cshtml

Views contain the html for displaying model data. Place this html under the "active" section.

<tr>
    <td class="adminTitle">
        @Html.NopLabelFor(model => model.AffiliateWebSite):
    </td>
    <td class="adminData">
        @Html.EditorFor(model => model.AffiliateWebSite)
        @Html.ValidationMessageFor(model => model.Active)
    </td>
</tr>
The controller

In this case the controller is responsible for mapping the domain data model to our view model and vice versa. The reason I choose the affiliate model to update is because of the simplicity. I want this to be an introduction to the nopCommerce platform and I would like to keep it as simple as possible.

File System Location: [Project Root]\Presentation\Nop.Web\Administration\Controllerss\AffiliateController.cs
Assembly: Nop.Admin
Solution Location: Nop.Admin.Controllers.AffiliateController.cs

We're going to make three updates to the AffiliateController class.

  • Data Model -> View Model
  • Create View Model -> Data Model
  • Edit View Model -> Data Model

Normally I would write tests for the following code and verify that model mapping is working correctly, but I'll skip unit testing to keep it simple.

In the method PrepareAffiliateModel add the following code below the model.Active = affiliate.Active:

// Data Model -> Ceate/Edit View Model
model.AffiliateWebSite = affiliate.AffiliateWebSite;

In the public ActionResult Create(AffiliateModel model, bool continueEditing) method add the following code below affiliate.Active = model.Active:

// Create View Model -> Data Model
affiliate.AffiliateWebSite = model.AffiliateWebSite;

A similar change is required in public ActionResult Edit(AffiliateModel model, bool continueEditing):

// Edit View Model -> Data Model
affiliate.AffiliateWebSite = model.AffiliateWebSite;
Troubleshooting
  • Recreate the database. Either your own custom SQL script or use the nopCommerce installer.
  • Stop the development web server between schema changes.
  • Post a detailed comment on our forums.

posted on 2012-05-08 21:07  philzhou  阅读(588)  评论(0编辑  收藏  举报