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

时间:2015-09-08 18:01:10

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

我正在为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;
    }
}

1 个答案:

答案 0 :(得分:0)

不确定您正在寻求什么样的答案。

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

forese的insead,你可以使用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();
}