2

Where should one place the Mapping profiler class? In the Data access layer folder? Business Service Layer folder? In the controller folder?

public class MappingProfile : Profile {
    public MappingProfile() {
        // Add as many of these lines as you need to map your objects
        CreateMap<User, UserDto>();
        CreateMap<UserDto, User>();
    }
}

4 Answers4

2

Its depends on your architecture design.

Different people prefer different place to place mapping profile classes.

My personal opinion I always create a project called AutoMapperMapping for placing all automapper related stuffs and refer the solution dll in the places which I need them.

If you are following three layer architecture then you can place them into the business layer. And if you are using auto mapper in your controller then you can place the mapping in a separate folder in your host project itself.

Edit : 25-OCT-2022

You can also create a class library for third party libraries and can add these libraries inside that class library.

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
1

Depends on the profile itself i.e.

  • what is being mapped ?
  • and where do you intent to use the mapping (on which layer) ?

If the only place where you are mapping is on the top-most tier, then probably the profile is best fit there,
so as to minimize the layers where you reference the automapper.

Askar Rayapov
  • 139
  • 1
  • 7
0

As mentioned here for ASP.net-MVC it's mostly a matter of choice based upon the solution architecture you have, even you can put it in an other project (e.g. domain project in onion architecture). For multi-layer architectures of course you may have multiple profiles and configurations.

but for asp.net core as in some tutorials you can have an Extension folder to put in Validation Attributes, Filter Attributes, Profile and etc for a single project solution. and for being consisted for dependency injection i will suggest to put a Imapperinterface in constructor of controller and call services.AddAutoMapper() in ConfigureServices of Startup.cs.

yekanchi
  • 813
  • 1
  • 12
  • 30
  • 1
    Domain is meant not to little dependencies on the other layers. First, AutoMapper is an external dependency thats closer to the infrastructure / boundaries than its to the domain, second, the DTOs are not part of the Domain itself if you put the mapper there you also have to reference the DTOs or put them in the domain itself, so thats pretty wrong imho – Tseng Oct 23 '18 at 06:03
0

It depends upon your need, i use it for mapping ViewModels with Code-First Model classes. I made a new folder/namespace Mappings such as yourProject.Mappings and placed class AutoMapperProfileConfiguration : Profilein it.

Ikram Shah
  • 1,206
  • 1
  • 11
  • 12