当源很复杂时,将域 Class 展平到 ViewModel
Flatten Domain Class To ViewModel When Source Is Complex
我正在使用 ValueInjecter 将域 类 映射到我的视图模型。我的域 类 很复杂。借用this question的例子:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
// VIEW MODEL
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int PersonId { get; set; }
public int AddressId { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
我查看了 FlatLoopInjection,但它希望视图模型 类 以嵌套域模型类型为前缀,如下所示:
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Id { get; set; }
public int AddressId { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
public string AddressZip { get; set; }
}
链接问题中的 OP 更改了他的视图模型以匹配 FlatLoopInjection 预期的约定。我不想那样做。
如何将我的域模型映射到原始的无前缀视图模型?我怀疑我需要覆盖 FlatLoopInjection 以删除前缀,但我不确定在哪里执行此操作。我查看了 FlatLoopInjection 的源代码,但不确定是否需要更改 Match 方法或 SetValue 方法。
不需要压平,先加贴图:
Mapper.AddMap<Person, PersonViewModel>(src =>
{
var res = new PersonViewModel();
res.InjectFrom(src); // maps properties with same name and type
res.InjectFrom(src.Address);
return res;
});
然后你可以调用:
var vm = Mapper.Map<PersonViewModel>(person);
我正在使用 ValueInjecter 将域 类 映射到我的视图模型。我的域 类 很复杂。借用this question的例子:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
// VIEW MODEL
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int PersonId { get; set; }
public int AddressId { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
我查看了 FlatLoopInjection,但它希望视图模型 类 以嵌套域模型类型为前缀,如下所示:
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Id { get; set; }
public int AddressId { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
public string AddressZip { get; set; }
}
链接问题中的 OP 更改了他的视图模型以匹配 FlatLoopInjection 预期的约定。我不想那样做。 如何将我的域模型映射到原始的无前缀视图模型?我怀疑我需要覆盖 FlatLoopInjection 以删除前缀,但我不确定在哪里执行此操作。我查看了 FlatLoopInjection 的源代码,但不确定是否需要更改 Match 方法或 SetValue 方法。
不需要压平,先加贴图:
Mapper.AddMap<Person, PersonViewModel>(src =>
{
var res = new PersonViewModel();
res.InjectFrom(src); // maps properties with same name and type
res.InjectFrom(src.Address);
return res;
});
然后你可以调用:
var vm = Mapper.Map<PersonViewModel>(person);