Difference between revisions of "AutoMapper"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:MVC Category:Core == Installation == Command line dotnet add package AutoMapper dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection --v...")
 
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
Command line  
 
Command line  
 
  dotnet add package AutoMapper
 
  dotnet add package AutoMapper
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection --version 1.2.0
 
 
PM>
 
PM>
 
  Install-Package AutoMapper
 
  Install-Package AutoMapper
== Add to Dependency Injection ==
+
== Creating Profile ==
In startup.cs -> ConfigurationServices add this line
+
This is a complex tree model structure
services.AddAutoMapper();
+
'''To create parent children mapping, create the child mapping first and then create parent children mapping. '''
add
+
CreateMap<Source, Target>
using AutoMapper;
+
<pre>
 +
  public class ApplicationProfile:AutoMapper.Profile
 +
    {
 +
        public ApplicationProfile()
 +
        {
 +
            CreateMap<Common.Models.Entities.VDS.Proposal, ProposalForm>();
 +
            CreateMap<Common.Models.Entities.VDS.Section, Models.Insurance.Section>();
 +
            CreateMap<Common.Models.Entities.VDS.ProposalQuestionAnswer, ProposalFormQuestionAnswer>()
 +
                .ForMember(dest => dest.ProposalFormQuestionAnswerPossibleAnswer, opt => opt.MapFrom(src => src.PossibleAnswer));
 +
            CreateMap<Common.Models.Entities.VDS.PossibleAnswer, ProposalFormQuestionAnswerPossibleAnswer>();
 +
 
 +
            CreateMap<Common.Models.Entities.VDS.Question, Models.Insurance.Question>()
 +
                .ForMember(dest => dest.QuestionType, opt => opt.MapFrom(src => src.QuestionType));
 +
 
 +
            CreateMap<Common.Models.Entities.VDS.QuestionType, Models.Insurance.QuestionType>();
 +
 
 +
            CreateMap<Common.Models.Entities.VDS.ProposalSection, Models.ProposalForm.ProposalFormSection>()
 +
                .ForMember(dest => dest.Section, opt => opt.MapFrom(src => src.Section))
 +
                .ForMember(dest => dest.Questions, opt => opt.MapFrom(src => src.ProposalQuestion));
 +
 
 +
            CreateMap<Common.Models.Entities.VDS.ProposalQuestion, Models.ProposalForm.ProposalFormQuestion>()
 +
                .ForMember(dest => dest.Question, opt => opt.MapFrom(src => src.Question))
 +
                .ForMember(dest => dest.QuestionAnswers, opt => opt.MapFrom(src => src.ProposalQuestionAnswer));
 +
 
 +
            CreateMap<Common.Models.Entities.VDS.Proposal, ProposalForm>()
 +
                .ForMember(dest => dest.ProposalFormSections, opt => opt.MapFrom(src=> src.VdsProposalSection));
 +
 
 +
        }
 +
    }
 +
</pre>
 +
 
 +
Also adding .ReverseMap() creates a reverse mapping
 +
CreateMap<Common.Models.Entities.VDS.QuestionType, Models.Insurance.QuestionType>().ReverseMap();
 +
 
 +
== Startup.cs ==
 +
In startup.cs -> ConfigurationServices add these lines after '''services.AddMvc'''
 +
<pre>
 +
  var autoMapperConfig = new AutoMapper.MapperConfiguration(c =>
 +
            {
 +
                c.AddProfile(new ApplicationProfile());
 +
            });
 +
            var mapper = autoMapperConfig.CreateMapper();
 +
            services.AddSingleton(mapper);
 +
</pre>
 
== usage ==
 
== usage ==
 
inject as '''IMapper mapper'''
 
inject as '''IMapper mapper'''
Line 19: Line 61:
  
 
Map like below. First parameter is source second is target and in parentheses, we provide the object
 
Map like below. First parameter is source second is target and in parentheses, we provide the object
  mapper.Map<List<Make>, List<MakeResource>>(makes)
+
  this.mapper.Map<Models.ProposalForm.ProposalForm>(proposal);
 +
 
 +
this.mapper.Map<target>(source);
 +
== ⚠️ Important for AutoMapper 12+ / 13+ / 15+ / 16+ ==
 +
 
 +
Recent versions of AutoMapper merged or changed DI behavior. Depending on your exact version:
 +
 
 +
If you're using AutoMapper ≥ 13, you may not need the extensions package anymore.
  
== Mapping Profile ==
+
In that case, the correct usage is:
'''Profile''' is in '''AutoMapper''' namespace
+
<pre>
<pre class="brush:CSharp;">
+
builder.Services.AddAutoMapper(cfg =>
public class MappingProfile : Profile
+
 
{
 
{
  public MappingProfile
+
     cfg.AddProfile<ApplicationProfile>();
  {
+
});
     CreateMap<Make, MakeResource>();
+
    CreateMap<Model, ModelResource>();
+
  }
+
}
+
Mapping profile is loaded during startup
+
 
</pre>
 
</pre>

Latest revision as of 10:47, 17 March 2026


Installation

Command line

dotnet add package AutoMapper

PM>

Install-Package AutoMapper

Creating Profile

This is a complex tree model structure To create parent children mapping, create the child mapping first and then create parent children mapping. CreateMap<Source, Target>

  public class ApplicationProfile:AutoMapper.Profile
    {
        public ApplicationProfile()
        {
            CreateMap<Common.Models.Entities.VDS.Proposal, ProposalForm>();
            CreateMap<Common.Models.Entities.VDS.Section, Models.Insurance.Section>();
            CreateMap<Common.Models.Entities.VDS.ProposalQuestionAnswer, ProposalFormQuestionAnswer>()
                .ForMember(dest => dest.ProposalFormQuestionAnswerPossibleAnswer, opt => opt.MapFrom(src => src.PossibleAnswer));
            CreateMap<Common.Models.Entities.VDS.PossibleAnswer, ProposalFormQuestionAnswerPossibleAnswer>();

            CreateMap<Common.Models.Entities.VDS.Question, Models.Insurance.Question>()
                .ForMember(dest => dest.QuestionType, opt => opt.MapFrom(src => src.QuestionType));

            CreateMap<Common.Models.Entities.VDS.QuestionType, Models.Insurance.QuestionType>();

            CreateMap<Common.Models.Entities.VDS.ProposalSection, Models.ProposalForm.ProposalFormSection>()
                .ForMember(dest => dest.Section, opt => opt.MapFrom(src => src.Section))
                .ForMember(dest => dest.Questions, opt => opt.MapFrom(src => src.ProposalQuestion));

            CreateMap<Common.Models.Entities.VDS.ProposalQuestion, Models.ProposalForm.ProposalFormQuestion>()
                .ForMember(dest => dest.Question, opt => opt.MapFrom(src => src.Question))
                .ForMember(dest => dest.QuestionAnswers, opt => opt.MapFrom(src => src.ProposalQuestionAnswer));

            CreateMap<Common.Models.Entities.VDS.Proposal, ProposalForm>()
                .ForMember(dest => dest.ProposalFormSections, opt => opt.MapFrom(src=> src.VdsProposalSection));

        }
    }

Also adding .ReverseMap() creates a reverse mapping

CreateMap<Common.Models.Entities.VDS.QuestionType, Models.Insurance.QuestionType>().ReverseMap();

Startup.cs

In startup.cs -> ConfigurationServices add these lines after services.AddMvc

  var autoMapperConfig = new AutoMapper.MapperConfiguration(c =>
            {
                c.AddProfile(new ApplicationProfile());
            });
            var mapper = autoMapperConfig.CreateMapper();
            services.AddSingleton(mapper);

usage

inject as IMapper mapper

public MakesController(VegaDbContext context, IMapper mapper)
...

Map like below. First parameter is source second is target and in parentheses, we provide the object

this.mapper.Map<Models.ProposalForm.ProposalForm>(proposal);

this.mapper.Map<target>(source);

⚠️ Important for AutoMapper 12+ / 13+ / 15+ / 16+

Recent versions of AutoMapper merged or changed DI behavior. Depending on your exact version:

If you're using AutoMapper ≥ 13, you may not need the extensions package anymore.

In that case, the correct usage is:

builder.Services.AddAutoMapper(cfg =>
{
    cfg.AddProfile<ApplicationProfile>();
});