【问题标题】:Entity to View Model Serialize in Framework Serialization在框架序列化中查看模型序列化的实体
【发布时间】:2015-09-08 18:01:09
【问题描述】:

我正在为 EF 中的客户实体创建视图模型。我的问题是我是否使用了正确的方法。我将实体属性转换为视图模型属性。而且,如果我需要返回的是一个列表,我会转换每个对象的属性。 有没有更好的方法来进行这种序列化?我不是在问将实体转换为模型是否是正确的方法。我不这样做,我只是返回需要的东西。我想知道是否有更好的方法将实体序列化为视图模型中的对象。

这是我的视图模型:

public class CustomerModel
{
    public int TotalRecords { get; set; }
    public int CUSTOMER_KEY { get; set; }
    public decimal CCUSTID { get; set; }
    public string CCNAME { get; set; }
    public string ACCNOTES { get; set; }
    public string CUSTPRCLEVEL_CODE { get; set; }
    public string CUSTPRCLEVEL_CODE_Name { get; set; }
    public DateTime LASTMODIFIEDDATE { get; set; }
    public string LASTMODIFIEDBY { get; set; }


    public static Customer FromModelToEntity(CustomerModel model)
    {
        Customer entity = new Customer();
        entity.CUSTOMER_KEY = model.CUSTOMER_KEY;
        entity.CCUSTID = model.CCUSTID;
        entity.CCNAME = model.CCNAME != null ? model.CCNAME : null;
        entity.ACCNOTES = model.ACCNOTES != null ? model.ACCNOTES : null;
        entity.CUSTPRCLEVEL_CODE = model.CUSTPRCLEVEL_CODE != null ? model.CUSTPRCLEVEL_CODE : null;           

        entity.LASTMODIFIEDDATE = model.LASTMODIFIEDDATE;
        entity.LASTMODIFIEDBY = model.LASTMODIFIEDBY != null ? model.LASTMODIFIEDBY : null;

        return entity;
    }

    public static CustomerModel FromEntityToModel(Customer entity)
    {
        CustomerModel model = new CustomerModel();
        model.CUSTOMER_KEY = entity.CUSTOMER_KEY;
        model.CCUSTID = entity.CCUSTID;
        model.CCNAME = entity.CCNAME != null ? entity.CCNAME : null;
        model.ACCNOTES = entity.ACCNOTES != null ? entity.ACCNOTES : null;
        model.CUSTPRCLEVEL_CODE = entity.CUSTPRCLEVEL_CODE != null ? entity.CUSTPRCLEVEL_CODE : null;
        model.CUSTPRCLEVEL_CODE_Name = entity.CustomerPricingLevel != null ? entity.CustomerPricingLevel.DESCRIPTION : string.Empty;

        model.LASTMODIFIEDDATE = entity.LASTMODIFIEDDATE;
        model.LASTMODIFIEDBY = entity.LASTMODIFIEDBY != null ? entity.LASTMODIFIEDBY : null;

        return model;
    }

    public static List<Customer> FromModelToEntity(List<CustomerModel> modelList)
    {
        List<Customer> entityList = new List<Customer>();
        foreach (var item in modelList)
        {
            entityList.Add(CustomerModel.FromModelToEntity(item));
        }
        return entityList;
    }

    public static List<CustomerModel> FromEntityToModel(List<Customer> entityList)
    {
        List<CustomerModel> modelList = new List<CustomerModel>();
        foreach (var item in entityList)
        {
            modelList.Add(CustomerModel.FromEntityToModel(item));
        }
        return modelList;
    }
}

【问题讨论】:

  • 也许看看 automapper,我建议将 Domain 对象和视图模型分开。他们需要能够单独进化
  • 分离域对象和视图模型是什么意思?我从哪里得到数据?你有链接解释吗?
  • CustomerModel 中有一个名为FromEntityToModel 的方法,它返回一个CustomerModel 紧密耦合您的Customer 域对象和您的CustomerViewModel。视图模型用于以视图期望的特定方式表示域,它不应该是 1:1 表示,如果是,您也可以在视图中使用 Domain 对象跨度>
  • 我也没有得到单独进化的东西。将您的模型“耦合”到您的域对象意味着您将捕获数据库更改可能产生的任何错误。在实体模型中将LASTMODIFIEDDATE 设为可为空的日期时间的那一刻,您就会发现错误。Automapper 会在您发布并尝试运行您的应用程序后通知您。或者它可能会完全忽略该字段。
  • 这不是 1:1 的表示,我在问题的描述中澄清了这一点。该模型将随着实体中不存在的属性一起增长。

标签: c# .net asp.net-mvc entity-framework


【解决方案1】:

不知道你在寻找什么样的答案。

不过,您可以使用 LINQ 缩短代码。 (System.Linq)

在 foreach 中,您可以使用 1 班轮:

这样:

public static List<Customer> FromModelToEntity(List<CustomerModel> modelList)
{
    List<Customer> entityList = new List<Customer>();
    foreach (var item in modelList)
    {
      entityList.Add(CustomerModel.FromModelToEntity(item));
    }
    return entityList;
}

变成这样:

public static List<Customer> FromModelToEntity(List<CustomerModel> modelList)
{
   return modelList.Select(item => FromModelToEntity(item)).ToList();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多