我想知道是否可以使用Automapper将多个DTO对象映射到单个ViewModel对象?
本质上,我有多个DTO对象,并希望在ASP.NET MVC 2.0中的单个屏幕上显示每个对象的信息。为此,我想将DTO对象(或其中的一部分......)展平到Viewmodel中,并将所述viewmodel传递给视图。如果我有一个DTO,这将很容易,但我从未见过它是用多个完成的。显然,有很多迂回的方法可以做到这一点(在automapper之外),但这是我想采取的方法,如果可能的话。
您可以创建一个复合DTO,它包含两个或多个DTO对象,并将复合DTO映射到输出视图模型。
如果您有2个DTO类和1个展平视图模型:
public class Dto1
{
public string Property1 { get; set; }
}
public class Dto2
{
public string Property2 { get; set; }
}
public class FlattenedViewModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
并为两个DTO创建映射以查看模型:
CreateMap<Dto1, FlattenedViewModel>();
CreateMap<Dto2, FlattenedViewModel>();
您可以将1st DTO映射到模型,然后只需“追加”第二个DTO:
var dto1 = new Dto1 { Property1 = "Value1"; }
var dto2 = new Dto2 { Property2 = "Value2"; }
var model = Mapper.Map<FlattenedViewModel>(dto1); // map dto1 properties
Mapper.Map(dto2, model); // append dto2 properties
您可以在带有params数组的IMappingEngine上添加Map覆盖扩展方法。就像是:
public static class AutoMapperExtensions
{
public static T Map<T>(this IMappingEngine engine, params object[] sources) where T : class
{
if (sources == null || sources.Length == 0)
return default(T);
var destinationType = typeof (T);
var result = engine.Map(sources[0], sources[0].GetType(), destinationType) as T;
for (int i = 1; i < sources.Length; i++)
{
engine.Map(sources[i], result, sources[i].GetType(), destinationType);
}
return result;
}
}
然后你可以像这样调用它:
var result = Mapper.Engine.Map<MyViewModel>(dto1, dto2, dto3);
我只是自己解决了这个问题,并提供了一个很好的解您的两个视图很可能在您的系统中以某种方式实际相关(特别是如果您使用的是Entity Framework)。检查您的模型,您应该看到显示关系的内容,如果您不这样做,则只需添加它。 (virtual
)
你的模特
public class Dto1
{
public int id { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
public string Property4 { get; set; }
public string Property5 { get; set; }
public virtual Dto2 dto2{ get; set; }
}
public class Dto2
{
public int id { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
public string PropertyD { get; set; }
public string PropertyE { get; set; }
}
你的ViewModels
public class Dto1ViewModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public virtual Dto2VMForDto1 dto2{ get; set; }
}
//Special ViewModel just for sliding into the above
public class Dto2VMForDto1
{
public int id { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
Automapper看起来像这样:
cfg.CreateMap< Dto1, Dto1ViewModel>();
cfg.CreateMap< Dto2, Dto2VMForDto1 >();
我假设您使用LinQ获取数据:
Dto1ViewModel thePageVM = (from entry in context.Dto1 where...).ProjectTo<Dto1ViewModel>();
中提琴,一切都会奏效。在您的视图中,只需使用model.dto2.PropertyB
访问
这是来自此答案中过期链接的信息:https://stackoverflow.com/a/8923063/2005596
当使用AutoMapper(http://automapper.codeplex.com)时,我经常会遇到需要将多个实体映射到一个实体的场景。通常,当从多个域实体映射到单个视图模型(ASP.NET MVC)时会发生这种情况。不幸的是,AutoMapper API没有公开将多个实体映射到一个实体的功能。但是创建一些辅助方法来执行此操作相对简单。下面我将说明我采取的方法。
在此示例中,我的域模型中包含以下实体
public class Person
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
}
public class Address
{
public int Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Country { get; set; }
}
public class Comment
{
public string Text { get; set; }
public DateTime Created { get; set; }
}
除此之外,我还要求在单个页面上使用ASP.NET MVC呈现人员详细信息的人员地址和任何相关注释。为了实现这一点,我创建了下面显示的视图模型,其中包括上面显示的所有三个域实体的数据
public class PersonViewModel
{
public int Id { get; set; }
[DisplayName("Firstname")]
public string Firstname { get; set; }
[DisplayName("Surname")]
public string Surname { get; set; }
[DisplayName("Address Line 1")]
public string AddressLine1 { get; set; }
[DisplayName("Address Line 2")]
public string AddressLine2 { get; set; }
[DisplayName("Country Of Residence")]
public string Country { get; set; }
[DisplayName("Admin Comment")]
public string Comment { get; set; }
}
在控制器操作方法中,我对域层进行三次单独调用以检索所需的实体,但这仍然存在我需要将多个源实体映射到单个目标实体的问题。为了执行这种映射,我创建了一个辅助类,它封装了AutoMapper并公开了一些功能,该功能支持将多个源对象映射到一个目标对象上。这个课程如下所示
public static class EntityMapper
{
public static T Map<T>(params object[] sources) where T : class
{
if (!sources.Any())
{
return default(T);
}
var initialSource = sources[0];
var mappingResult = Map<T>(initialSource);
// Now map the remaining source objects
if (sources.Count() > 1)
{
Map(mappingResult, sources.Skip(1).ToArray());
}
return mappingResult;
}
private static void Map(object destination, params object[] sources)
{
if (!sources.Any())
{
return;
}
var destinationType = destination.GetType();
foreach (var source in sources)
{
var sourceType = source.GetType();
Mapper.Map(source, destination, sourceType, destinationType);
}
}
private static T Map<T>(object source) where T : class
{
var destinationType = typeof(T);
var sourceType = source.GetType();
var mappingResult = Mapper.Map(source, sourceType, destinationType);
return mappingResult as T;
}
}
要将多个源对象映射到一个目标,我已经使用了AutoMapper提供的功能,它允许您在源对象和已存在的目标对象之间执行映射。
最后,下面是控制器中的代码,该代码检索三个实体并执行映射到单个视图模型
public ActionResult Index()
{
// Retrieve the person, address and comment entities and
// map them on to a person view model entity
var personId = 23;
var person = _personTasks.GetPerson(personId);
var address = _personTasks.GetAddress(personId);
var comment = _personTasks.GetComment(personId);
var personViewModel = EntityMapper.Map<PersonViewModel>(person, address, comment);
return this.View(personViewModel);
}