Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#
Tip/Trick

A Recap about Creating Maps for AutoMapper in .NET Core

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
2 Oct 2018CPOL1 min read 9.1K   4   1
Different options to create the maps

Introduction

I have an ASP.NET Core MVC Web Applications with models and view models fitting the architecture of the database and fields required in each view. I choose Automapper to simplify the conversion between the object from the database and to the view. In some cases, the conversion is one to one, in others, one view models should be converted to many domain models.

Background

This link explains how to get started with Automapper in .NET Core. Also, here you can read about creating maps for different scenarios. Additionally, there are many comments on Stackoverflow about using tuples to create one to many classes maps.

Using the Code

First, you can create a map for a conversion from one domain model to one view model as follows:

JavaScript
CreateMap<ViewModels.Base, Models.Base>()
   .ForMember(dest => dest.BaseId, opts => opts.MapFrom(src => src.BaseId))
   .ForMember(dest => dest.UserId, opts => opts.MapFrom(src => src.UserId))
   .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name));

CreateMap<ViewModels.Base, Models.Base>().ReverseMap();

If field names do not match, you need to specify them. You can define the reverse map.

On the other hand, for the case where a view model should be mapped with two domain models, you can use tuples as the following example.

JavaScript
CreateMap<System.Tuple<Models.Base, Models.BaseStatus>, ViewModels.Base>()
  .ForMember(dest => dest.BaseId, opts => opts.MapFrom(src => src.Item1.BaseId))
  .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Item1.Name))
  .ForMember(dest => dest.UserId, opts => opts.MapFrom(src => src.Item1.UserId))
  .ForMember(dest => dest.IsConnected, opts => opts.MapFrom(src => src.Item2.IsConnected))
  .ForMember(dest => dest.IsPlugged, opts => opts.MapFrom(src => src.Item2.IsPlugged));

For the reverse map, you will in each case pick the fields corresponding to each class:

JavaScript
CreateMap<ViewModels.Base, Models.Base>()
  .ForMember(dest => dest.BaseId, opts => opts.MapFrom(src => src.BaseId))
  .ForMember(dest => dest.UserId, opts => opts.MapFrom(src => src.UserId))
  .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name));

CreateMap<ViewModels.Base, Models.BaseStatus>()
  .ForMember(dest => dest.BaseId, opts => opts.MapFrom(src => src.BaseId))
  .ForMember(dest => dest.IsConnected, opts => opts.MapFrom(src => src.IsConnected))
  .ForMember(dest => dest.IsPlugged, opts => opts.MapFrom(src => src.IsPlugged));

Another option, for the one to many mappings could be using class composition. As soon as Models.Base and ViewModels.Base contains a field that is the Models.BaseStatus.

JavaScript
CreateMap<Models.Base, ViewModels.Base>()
  .ForMember(dest => dest.BaseId, opts => opts.MapFrom(src => src.BaseId))
  .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name))
  .ForMember(dest => dest.UserId, opts => opts.MapFrom(src => src.UserId))
  .ForMember(dest => dest.Status, opts => opts.MapFrom(src => new BaseStatus
            {
                  IsConnected = src.IsConnected,
                  IsPlugged = src.IsPlugged))
             };

Finally, this is the way the maps can be called:

JavaScript
ViewModels.Base vmb = _mapper.Map<BaseViewModel>(new Tuple<Models.Base, Models.BaseStatus>(b, bs));
ViewModels.Base vmb = _mapper.Map<Models.Base>(b);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTuple types Pin
jimmy.bogard3-Oct-18 6:08
jimmy.bogard3-Oct-18 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.