1
我有一個類似的嵌套的視圖模型:Automapper如何從嵌套模型工作到平面模型?
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;}
}
然後,有一個員工的域對象,像這樣:
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));
正如你可以看到,在職工域對象的屬性名稱和AddressViewModel是相同。所以,似乎應該有一個更簡單的方法來做到這一點。
感謝
這應該被標記爲答案。完美契合。我不知道它是如何工作的,它也解決了我也遇到的類似問題。 – julealgon 2014-10-10 15:04:28