Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring AutoMapper in Multiple Projects

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?

like image 350
GregH Avatar asked Aug 03 '17 19:08

GregH


People also ask

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

Where do I configure AutoMapper?

Create a MapperConfiguration instance and initialize configuration via the constructor: var config = new MapperConfiguration(cfg => { cfg. CreateMap<Foo, Bar>(); cfg. AddProfile<FooProfile>(); });

Is AutoMapper faster than manual mapping?

Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full . NET Framework).

What can I use instead of AutoMapper?

AutoMapper is one of the popular object-object mapping libraries with over 296 million NuGet package downloads. It was first published in 2011 and its usage is growing ever since. Mapster is an emerging alternative to AutoMapper which was first published in 2015 and has over 7.4 million NuGet package downloads.


2 Answers

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

like image 185
ironstone13 Avatar answered Oct 09 '22 16:10

ironstone13


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.

like image 40
jmoerdyk Avatar answered Oct 09 '22 16:10

jmoerdyk