映射IEnumerable模型进行查看

时间:2014-02-19 12:52:59

标签: c# asp.net-mvc automapper

保持这个基本我有两个模型。 customer和CustomerViewModel。我想将属性映射到另一个。

public class Customer
{
    public string CompanyName { get; set; }
    public int CustomerType { get; set; }
    public IEnumerable<ContactViewModel> CustomerContacts { get; set; }
}

public class CustomerViewModel
{
    public string CompanyName { get; set; }
    public int CustomerType { get; set; }
    public ContactName {get;set;}
    public ContactTel {get;set;}
}

问题是我想将customerContact映射到contactName和contactTel。据我所知,它是一个IEnumerable并包含一个联系人列表,但在此过滤器中IEnumerable的联系人视图模型只包含1条记录。

我如何在automapper中进行这种类型的映射?

更新:

我决定和前面提到过的类似approuch一起去。我希望我能在自动播放器中做到这一点,但我想它稍微复杂一些。

public static CustomerListViewModel MapToListView(this CustomerServiceModel svcModel)
    {
        ContactServiceModel contact = new ContactServiceModel { Name = String.Empty, Telephone = String.Empty }; 
        if(svcModel.CustomerContacts != null) contact = svcModel.CustomerContacts.FirstOrDefault();

        return new CustomerListViewModel
        {
            Id = svcModel.Id,
            address = svcModel.Address.MapToView(),
            CompanyName = svcModel.CompanyName,
            ContactName = contact.Name,
            ContactTelephone = contact.Telephone
        };
    }

3 个答案:

答案 0 :(得分:1)

我不确定AutoMapper可以做到这一点。您可以尝试定义扩展方法,例如:

public static CustomerViewModel ToViewModel(this Customer cust)
        {
            return new CustomerViewModel()
                {
                    CompanyName = cust.CompanyName,
                    CustomerType = cust.CustomerType,
                    ContactName = cust.CustomerContacts.First().ContactName,
                    ContactTel = cust.CustomerContacts.First().ContactTel
                };
        }

当然,在顶部进行一点验证会很好。 然后你就像这样使用它:

Customer cust= GetCustomer();
CustomerViewModel model= cust.ToViewModel();

答案 1 :(得分:0)

尝试使用linq

public ActionResult Index()
{
   var viewmodel = CustomerViewModel();

   return View(viewmodel);
}

private static CustomerViewModel ()
{
    return new CustomerViewModel
    {
    ...
    ContactName = BusinessLogic.GetContactName("Microsoft");
    ContactTel = BusinessLogic.GetContactTel();
    }
}

BusinessLogic是您的“大脑”类,它决定从数据中获取什么,并使用linq获取电话和名称:

public static class BusinessLogic
{
    public static string GetContactName(string companyName)
    {
        var c = new Customer () ... // get you Customer-object

        var q = (from contacts in c.CustomerContacts
                where contacts.CompanyName == companyName
                select contacts.ContactName).First();

        return q;
    }
}

但是你可以看到一个错误。您混淆视图模型数据(您希望在网页上为用户显示)和数据模型(显示对象模型“联系人”)。换句话说..最好将特殊模型类用作CustomerContacts的Type。抱歉,我的“好”英语。希望我帮助过你。

答案 2 :(得分:0)

如果CustomerContactsnull或为空(我将尝试提出更强大的解决方案),这将抛出异常,但这里有一些适用于快乐路径的内容:

Mapper.CreateMap<Customer, CustomerViewModel>()
    .ForMember(dest => dest.ContactName, opt => opt.MapFrom(src => src.CustomerContacts.First().ContactName))
    .ForMember(dest => dest.ContactTel, opt => opt.MapFrom(src => src.CustomerContacts.First().ContactTel));

也许更安全的选择是将逻辑置于Customer类本身:

public class Customer
{
    /* other members */
    public string ContactName 
    { 
        get 
        {
            string name = null;
            if (CustomerContacts != null && CustomerContacts.Any()) 
            {
                name = CustomerContacts.First().ContactName;
            }

            return name;
        }
    }

    public string ContactTel
    {
        get 
        {
            string tel = null;


            if (CustomerContacts != null && CustomerContacts.Any()) 
            {
                tel = CustomerContacts.First().ContactTel;
            }
            return tel;
        }
    }

}