繁体   English   中英

在框架序列化中查看模型序列化的实体

[英]Entity to View Model Serialize in Framework Serialization

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

这是我的视图模型:

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;
    }
}

不确定要寻找哪种答案。

不过,您可以使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM