Search code examples
aspnetboilerplateasp.net-boilerplate

How to use Custom mapper with IObjectMapper on the service


Work on aspnetboilerplate asp.net core project, face difficulties on custom mapping. Want to create custom map and want to use it several times as like AUTOMAP profiler.Follow documentation but failed to implement this on my project. My steps are

1)Create a class MyModule under the XXX.Core

 [DependsOn(typeof(AbpAutoMapperModule))]
    public class MyModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
            {
                config.CreateMap<CreateUserInput, CreatUserOutput>()
                      .ForMember(u => u.Password, options => options.Ignore())
                      .ForMember(u => u.OutputEmailAddress, options => options.MapFrom(input => input.EmailAddress));
            });
        }
    }



    public class CreateUserInput
    {
        public string Name { get; set; }

        public string Surname { get; set; }

        public string EmailAddress { get; set; }

        public string Password { get; set; }
    }

    public class CreatUserOutput
    {
        public string OutputName { get; set; }

        public string Surname { get; set; }

        public string OutputEmailAddress { get; set; }

        public string Password { get; set; }
    }

2)Used above configuration on xxx.Application, service as like bellow

try
            {
                CreateUserInput te = new CreateUserInput
                {
                    EmailAddress = "[email protected]",
                    Name = "input",
                    Password = "test",
                    Surname = "sure"
                };

                CreatUserOutput ot = new CreatUserOutput();

                var temp = _objectMapper.Map<CreatUserOutput>(te);
            }
            catch (System.Exception ex)
            {


            }

I don't understand how to use custom mapper with my injected IObjectMapper on the service.


Solution

  • Better to create separate automapper profile. You need to create file exactly in Application layer You can name it for example AutoMapperProfile.cs with following:

     public class AutoMapperProfile : AutoMapper.Profile {
         public AutoMapperProfile () {
    
             CreateMap<CreateUserInput, CreatUserOutput> ()
                 .ForMember (u => u.Password, options => options.Ignore ())
                 .ForMember (u => u.OutputEmailAddress, options => options.MapFrom (input => input.EmailAddress));
    
         }
     }
    

    to order this code works make sure that you ApplicationModule.cs contain following code which responsible for loading profiles.

    public override void Initialize () {
        var thisAssembly = typeof (LicenseManagerApplicationModule).GetAssembly ();
    
        IocManager.RegisterAssemblyByConvention (thisAssembly);
    
        Configuration.Modules.AbpAutoMapper ().Configurators.Add (
            // Scan the assembly for classes which inherit from AutoMapper.Profile
            cfg => cfg.AddProfiles (thisAssembly)
        );
    }