Search code examples
c#.netasp.net-coreautomapper

How to refactor a AutoMapper function in Net Core


I'm resolving a Software Developer entrance exam where I have to develop a Student Enroll courses app, and in one of the stages wherein it requires to write a Mapper function that map a Model with his ModelDto. I did this, but now I have to refactor in order with the Omnio(clean architecture) methodology software development.

This is the Subject model:

  public class Subject
    {
        public int? SubjectId {get; set; }
        public string? Title {get; set; }
        public int? Credits {get; set; }
        public string registerAt? {get; set;}
   }

public class SubjectDto

 public string? Title {get; set; }
 public int? Credits {get; set; }
 public string registerAt? {get; set;}

Now this is the Mapper config I settle down:

public class MapperConfig:Profile
        {
            public static MapperConfiguration InitializeAutoMapper()
            {
                //Provide all the Mapping Configuration
                var config = new MapperConfiguration(cfg =>
                {
                    //Configuring Subject and EmployeeDTO
                    cfg.CreateMap<Subject, SubjectDto>()
                    //Provide Mapping Configuration of FullName and Name Property
                    .ForMember(dest => dest.Title, act => act.MapFrom(src => src.Title))
                    
                    //Provide Mapping Dept of FullName and Department Property
                    .ForMember(dest => dest.Credits, act => act.MapFrom(src => src.Credits));
                    //Any Other Mapping Configuration ....
                });
                return config;
            }
        }

And this is how I used it in controller:

   public async Task<IEnumerable<SubjectDto>> GetAllAsync(int id, CancellationToken cancellationToken = default)
        {
          var subjects = await _context.Subject.GetSubjectsAsync(id, cancellationToken);
          
          if (subjects is null)
          {
             throw new SubjectNotFoundException(id);
          }

          var mapper = MapperConfig.InitializeAutoMapper();

          var subjectDto = mapper.Map<SubjectDto>(subjects); #cause the error CS1061     
        }

It return me that MapperConfig doesnt have a extensible method for Map.

If anyone could help to correctly refactor the Mapper function, I'd be really grateful.


Solution

  • Adjust your configuration class:

    public class MapperConfig
    {
        public static Mapper InitializeAutoMapper()
        {
            //Provide all the Mapping Configuration
            var config = new MapperConfiguration(cfg =>
            {
                //Configuring Subject and EmployeeDTO
                cfg.CreateMap<Subject, SubjectDto>()
                //Provide Mapping Configuration of FullName and Name Property
                .ForMember(dest => dest.Title, act => act.MapFrom(src => src.Title))
    
                //Provide Mapping Dept of FullName and Department Property
                .ForMember(dest => dest.Credits, act => act.MapFrom(src => src.Credits));
                //Any Other Mapping Configuration ....
            });
    
            var mapper = new Mapper(config);
    
            return mapper;
        }
    }
    

    Don't forget to correct your models:

    public string registerAt? {get; set;}
    

    to

    public string? registerAt {get; set;}
    

    and finally, in your case, make the map for the list you are returning:

     var subjects = mapper.Map<IEnumerable<SubjectDto>>(subjects)
     retunr subjects ;
    

    I hope I was helpfull