简体   繁体   中英

Using Automapper with a collection of abstract objects

Please take a look at this rather contrived example of what I'm trying to do.

First, the database models:

public class Report
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<Worker> Workers { get; set; } 
}

public abstract class Worker
{
    public Guid Id { get; set; }
}

public class Fireman : Worker
{
    public string Station { get; set; }
}

public class Cleaner : Worker
{
    public string FavoriteSolvent { get; set; }
}

Now the view models:

public class AddReportViewModel
{
    public string Name { get; set; }
    public List<AddFiremanViewModel> Firemen { get; set; }
    public List<AddCleanerViewModel> Cleaners { get; set; }
}

public class AddFiremanViewModel
{
    public string Station { get; set; }
}

public class AddCleanerViewModel
{
    public string FavoriteSolvent { get; set; }
}

And finally the Automapper profile:

public class ReportProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<AddReportViewModel, Report>();
        CreateMap<AddFiremanViewModel, Fireman>();
        CreateMap<AddCleanerViewModel, Cleaner>();
    }
}

I want the Firemen and Cleaners to both populate the Workers collection, which is an ICollection<Worker> . I hope this makes sense. How can I do this?

Your view model doesn't contain any Id field so I set those up as Ignore in the mapping. Then, I just used LINQ's Union clause to combine the two source lists into a single collection (after converting each one using Automapper). Here's the mapping:

Mapper.CreateMap<AddReportViewModel, Report>()
    .ForMember(d => d.Id, o => o.Ignore())
    .ForMember(d => d.Workers, o => o.MapFrom(
        s => Mapper.Map<ICollection<AddFiremanViewModel>, ICollection<Fireman>>(s.Firemen)
            .Union<Worker>(Mapper.Map<ICollection<AddCleanerViewModel>, ICollection<Cleaner>>(s.Cleaners))))
    ;
Mapper.CreateMap<AddFiremanViewModel, Fireman>()
    .ForMember(d => d.Id, o => o.Ignore())
    ;
Mapper.CreateMap<AddCleanerViewModel, Cleaner>()
    .ForMember(d => d.Id, o => o.Ignore())
    ;

And here's an example of using it:

var vm = new AddReportViewModel
{
    Name = "Sample",
    Cleaners = new List<AddCleanerViewModel>
    {
        new AddCleanerViewModel {FavoriteSolvent = "Alcohol"}
    },
    Firemen = new List<AddFiremanViewModel>
    {
        new AddFiremanViewModel {Station = "51"},
        new AddFiremanViewModel {Station = "49"}
    }
};

var db = Mapper.Map<AddReportViewModel, Report>(vm);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM