20

我想知道是否可以使用 Automapper 将多个 DTO 对象映射到单个 ViewModel 对象?

本质上,我有多个 DTO 对象,并希望在 ASP.NET MVC 2.0 的单个屏幕上显示来自每个对象的信息。为此,我想将 DTO 对象(或它们的一部分......)展平到 Viewmodel 中,并将所述 viewmodel 传递给视图。如果我有一个 DTO,这将很容易,但我从未见过它是由多个完成的。显然有许多迂回的方法可以做到这一点(在自动映射器之外),但如果可能的话,这是我想采用的方法。

4

6 回答 6

14

检查有关您的查询的以下链接

http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx

于 2012-01-19T08:31:48.913 回答
10

您可以创建一个包含两个或多个 DTO 对象的复合 DTO,并将复合 DTO 映射到输出视图模型。

于 2010-01-24T16:34:41.837 回答
9

如果您有 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>();

您可以将第一个 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
于 2015-05-21T10:49:42.743 回答
4

您可以在采用 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);
于 2012-11-14T15:51:11.177 回答
1

这是此答案中过期链接的信息: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);

    }
于 2019-04-11T20:01:06.840 回答
0

我自己解决了这个问题,并有一个很好的解决方案。您的两个视图很可能实际上在您的系统中以某种方式相关(尤其是如果您使用的是实体框架)。检查您的模型,您应该会看到显示关系的内容,如果您不这样做,则只需添加它。(中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; }
    }

你的 ViewModel

    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; }
    }

自动映射器看起来像这样:

        cfg.CreateMap< Dto1, Dto1ViewModel>();
        cfg.CreateMap< Dto2, Dto2VMForDto1 >();

我假设您正在使用 LinQ 获取数据:

Dto1ViewModel thePageVM = (from entry in context.Dto1 where...).ProjectTo<Dto1ViewModel>();

维奥拉,一切都会好起来的。在您看来,只需使用model.dto2.PropertyB

于 2016-07-08T02:03:36.713 回答