Model view controller Automapper如何从嵌套模型工作到平面模型?
我的嵌套视图模型与此类似:Model view controller Automapper如何从嵌套模型工作到平面模型?,model-view-controller,dns,nested,automapper,Model View Controller,Dns,Nested,Automapper,我的嵌套视图模型与此类似: public class EmployeeViewModel { //... public string EmployeeFirstName { get; set; } public string EmployeeLastName { get; set; } public AddressViewModel{ get; set; } } AddressViewModel如下所示: public class Addres
public class EmployeeViewModel
{
//...
public string EmployeeFirstName { get; set; }
public string EmployeeLastName { get; set; }
public AddressViewModel{ get; set; }
}
AddressViewModel如下所示:
public class AddressViewModel
{
public string Street {get; set;}
public string City {get; set;}
public string State {get; set;}
public string Zip {get; set;}
}
然后,有一个Employee域对象,如下所示:
public class Employee
{
public string EmployeeFirstName { get; set; }
public string EmployeeLastName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
我正在尝试将EmployeeViewModel映射到Employee域对象。这就是我提出的方法,它是有效的,但我想知道是否有更简单的方法来做到这一点:
Mapper.CreateMap<EmployeeViewModel, Employee>().ForMember(destination => destination.Street, opt => opt.MapFrom(src => src.AddressViewModel.Street))
.ForMember(destination => destination.City, opt => opt.MapFrom(src => src.AddressViewModel.City))
.ForMember(destination => destination.State, opt => opt.MapFrom(src => src.AddressViewModel.State))
.ForMember(destination => destination.Zip, opt => opt.MapFrom(src => src.AddressViewModel.Zip));
Mapper.CreateMap().ForMember(destination=>destination.Street,opt=>opt.MapFrom(src=>src.AddressViewModel.Street))
.ForMember(destination=>destination.City,opt=>opt.MapFrom(src=>src.AddressViewModel.City))
.ForMember(destination=>destination.State,opt=>opt.MapFrom(src=>src.AddressViewModel.State))
.ForMember(destination=>destination.Zip,opt=>opt.MapFrom(src=>src.AddressViewModel.Zip));
如您所见,Employee域对象和AddressViewModel中的属性名称相同。所以,似乎应该有一个更简单的方法来做到这一点
谢谢您可以在文档中签出。下面是一个例子:
public class AddressViewModel
{
public string Street { get; set; }
}
public class EmployeeViewModel
{
public string EmployeeFirstName { get; set; }
public AddressViewModel Address { get; set; }
}
public class Employee
{
public string EmployeeFirstName { get; set; }
public string AddressStreet { get; set; }
}
class Program
{
static void Main()
{
Mapper.CreateMap<EmployeeViewModel, Employee>();
var result = Mapper.Map<EmployeeViewModel, Employee>(new EmployeeViewModel
{
EmployeeFirstName = "first name",
Address = new AddressViewModel
{
Street = "some street"
}
});
Console.WriteLine(result.EmployeeFirstName);
Console.WriteLine(result.AddressStreet);
}
}
公共类AddressViewModel
{
公共字符串Street{get;set;}
}
公共类EmployeeViewModel
{
公共字符串EmployeeFirstName{get;set;}
公共地址ViewModel地址{get;set;}
}
公营雇员
{
公共字符串EmployeeFirstName{get;set;}
公共字符串AddressStreet{get;set;}
}
班级计划
{
静态void Main()
{
CreateMap();
var result=Mapper.Map(新的EmployeeViewModel
{
EmployeeFirstName=“first name”,
Address=新的AddressViewModel
{
Street=“某条街”
}
});
Console.WriteLine(result.EmployeeFirstName);
控制台。写入线(结果。地址街);
}
}
请注意,对于展开,目标属性被称为
AddressStreet
,这应该标记为答案。非常合身。我不知道它是如何工作的,它也解决了我遇到的类似问题。