开发者

Is it possible to map multiple DTO objects to a single ViewModel using Automapper?

开发者 https://www.devze.com 2022-12-17 04:33 出处:网络
I was wondering if it is possible to map multiple DTO objects to a single ViewModel object using Automapper?

I was wondering if it is possible to map multiple DTO objects to a single ViewModel object using Automapper?

Essentiall开发者_运维技巧y, I have multiple DTO objects and would like to display information from each on a single screen in ASP.NET MVC 2.0. To do so I would like to flatten the DTO objects (or parts of them...) into the Viewmodel and pass said viewmodel to the view. If I had one DTO this would be easy, but I've never seen it being done with multiple. Obviously there are a number of roundabout ways to do this (outside of automapper), but this is the approach that I would like to take if possible.


Check for following link regarding your query

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


You could create a composite DTO that holds two or more DTO objects and map the composite DTO to the output view model.


If you have 2 DTO classes and 1 flattened view model:

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

And you create mappings for both DTOs to view model:

CreateMap<Dto1, FlattenedViewModel>();
CreateMap<Dto2, FlattenedViewModel>();

You can map 1st DTO to the model and then just "append" 2nd 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


You can add a Map override extension method off IMappingEngine that takes a params array. Something like:

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

You could then call it like this:

var result = Mapper.Engine.Map<MyViewModel>(dto1, dto2, dto3);


This is the information from the expired link in this answer: https://stackoverflow.com/a/8923063/2005596

When using AutoMapper (http://automapper.codeplex.com ) I often have a scenario where I need to map several entities into one entity. Commonly this occurs when mapping from a number domain entities into a single view model (ASP.NET MVC). Unfortunately the AutoMapper API does not expose functionality to map several entities into one entity; however it is relatively simple to create some helper method to do this. Below I will illustrate the approach that I have taken.

In this example I have the following entities in my domain model

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

}

In addition to this I have a requirement to render the person details the person’s address and any related comment on a single page (using ASP.NET MVC). To implement this I have created the view model shown below, which includes data from all three of the domain entities shown above

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



}

In the controller action method I make three separate calls into the domain layer to retrieve the required entities but this still leaves the issue that I need to map several source entities onto a single destination entity. To perform this mapping I created a helper class which encapsulates AutoMapper and exposes functionality which enables the mapping of several source objects onto one destination object. This class is shown below

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;

    }

}

To map several source objects onto one destination I have made use of the functionality provided by AutoMapper which allows you to perform a mapping between a source object and a destination object that already exists.

Finally below is the code from the controller that retrieves the three entities and performs the mapping onto a single view model

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

    }


I just worked this out myself and have a great solution. It is most likely that your two views are actually related somehow in your system (especially if you are using Entity Framework). Check your models and you should see something which displays the relationship, if you don't then just add it. (The virtual)

Your models

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

Your 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 looks like this:

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

I'm assuming you are getting your data with LinQ:

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

Viola, everything will work. In your view just access by using model.dto2.PropertyB

0

精彩评论

暂无评论...
验证码 换一张
取 消