zoukankan      html  css  js  c++  java
  • 扩展现有实体添加新的属性

    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.

    本教程将为代理商实体Affiliate entity添加一个属性,附带nopCom源码。

    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.

    实体将有两个类用于映射记录表:第一个类定义affiliate的属性、字段和方法。

    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.

    第二个类是将各属性分别映射到对应的SQL列,以及映射不同的SQL表之间的关系。

    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.

    <!--[if !supportFields]-->1<!--[endif]-->为Affiliate添加一个属性:

    // 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.

    <!--[if !supportFields]-->2<!--[endif]-->为AffiliateMap添加一个构造函数:

    // 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.

    <!--[if !supportFields]-->3<!--[endif]-->修改数据库,为Affiliate表添加列:AffiliateWebSite,允许为空,navrchar(255)。

    <!--[if !supportFields]-->4<!--[endif]-->重新编译程序

    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.

    表示模型用于传输控制器的信息视图(参考asp.net/mvc)。模型的另一个目的,定义需求。

    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.

    我们在数据库中设定AffiliateWebSite长度为255个字符,如果尝试保存300个字符的,程序将中断(或截断文本)。因此需要通过程序强制用户输入不超过255个字符,尽可能地降低出错。

    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.

    <!--[if !supportFields]-->5<!--[endif]-->添加视图模型需要的属性:

    // 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.

    <!--[if !supportFields]-->6<!--[endif]-->在视图中添加一行:

    <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.

    我们将要进行三次更新AffiliateController类。

    数据模型 - 视图模型

    创建视图模型 - >数据模型

    编辑视图模型 - >数据模型

    通常情况下,我会写下面的代码测试和验证模型的映射正常工作,但我会跳过单元测试,以保持它的简单。

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

    <!--[if !supportFields]-->7<!--[endif]-->找到Private void PrepareAffiliateModel方法,在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:

    <!--[if !supportFields]-->8<!--[endif]-->找到public ActionResult Create(AffiliateModel model, bool continueEditing)方法,在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):

    <!--[if !supportFields]-->9<!--[endif]-->最后,在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.
  • 相关阅读:
    nginx基础系列
    常用MS-SQL写法整理
    Spring Bean装配方式
    sql获取该周的开始结束日期
    Docker基础入门实践
    vim常规操作
    基于CentOS的SSHD服务的Docker镜像
    RedisClient For .Net
    Redis数据类型及使用场景
    CentOS下安装Redis
  • 原文地址:https://www.cnblogs.com/Bany/p/2729544.html
Copyright © 2011-2022 走看看