8

I currently have built an MVC solution which has a web project (controllers/views/scripts), services project (business layer, builds view models), and repositories project (data access layer).

I have used AutoMapper in my projects in the past and am trying to configure AutoMapper in this solution. Normally, I configure all of my maps in MapperConfig.cs which is called in Global.asax.cs.

My Problem is that the web project which is where I normally configure AutoMapper only has reference to the services project and the services project only has reference to the data project. So, when I go to configure my maps as I normally would, I am unable to define maps for the data project due to the web project not having a reference to the data project. I need a way to configure my data access layer maps without adding a reference for the data project to the web project.

The project dependency diagram would look like the following:

Web Proj --> Services Proj --> Data Proj

How can I overcome this?

GregH
  • 5,125
  • 8
  • 55
  • 109
  • Why do you want to have only one config for auto mapper? Do you really have classes / dtos that span all your layers? – ironstone13 Aug 03 '17 at 19:06
  • I have no classes that span all of my layers. I haven't been able to find a way to define maps on a per project basis - that is what I'm trying to overcome. With Ninject, I did this by creating classes in each project that inherit from `NinjectModule`. If you can post an AutoMapper example, I will gladly accept – GregH Aug 03 '17 at 19:08
  • How about using a static constructor that would init the maps in each project - see https://stackoverflow.com/questions/30136415/where-to-place-automapper-map-registration-in-referenced-dll – ironstone13 Aug 03 '17 at 19:10
  • Or even this - https://stackoverflow.com/questions/26458731/how-to-configure-auto-mapper-in-class-library-project – ironstone13 Aug 03 '17 at 19:11
  • Hey, I've written an answer, please let me know if you need any help – ironstone13 Aug 03 '17 at 19:33
  • @ironstone13 im working on a solution now. if it works for me, I'll gladly accept – GregH Aug 03 '17 at 19:46

2 Answers2

7

There is no need to have a single mapping registration file across all projects, especially that you say that you don't have any cross-cutting types.

The simplest way would be to define a configuration file per project, and have those configurations call each other, repeating the layered dependencies of your assemblies, like below:

Global.asax.cs --> WebProjMapRegistrations.Register()-->ServicesMapRegistrations.Register()-->DataMapRegistrations.Register()

Alternatively, you could use the Assembly Scanning for auto configuration

As described by @Jimmy Bogard, when you run your web app, all assemblies of your application will eventually get loaded into your application domain - so you can get all the profiles from all the assemblies and add them to mapper config: How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

Yet another alternative approach, that works for ASP.NET apps can be found here: Where to place AutoMapper map registration in referenced dll

Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
ironstone13
  • 3,325
  • 18
  • 24
  • So, in my web project on startup do I scan my assemblies across the multiple projects and map registrations that exist in each of the projects, is that correct? – Ciaran Gallagher Nov 30 '18 at 10:30
  • 1
    @CiaranGallagher using this method, you would call a method that registers your web project's maps from `global.asax.` and that method would register all maps needed in teh web project and also call a method which exists in the service layer called `ServicesMapRegistrations.Register()`. The service layer method would register all maps needed in the service layer as well as call `DataMapRegistrations.Register()` in the data project to register all maps in the data layer – GregH Nov 30 '18 at 13:39
1

The way I've handled this in some ASP.Net MVC projects, is by using AutoMapper Profiles.

You create separate mapping Profiles that handle creating the Mappings for objects in that Project/Assembly.

You then add the profiles to the overall configuration manually, or you can use Reflection/Assembly scanning to automatically load the profiles.

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49