AutoMapper Service Level

I have an MVC project where I use AutoMapper to map Entity Framework objects to view models. The code that defines the mappings is in the boostrapper class, which is called automatically when the application starts (App_Start, Global.asax)

I am refactoring my code to put all my business logic at the service level, because we need to implement a batch process that runs daily, which runs the same logic as the MVC application.

One of the problems I am facing now is that I need to map the objects of my database to some domain objects at my service level. I think that everything will work well in an MVC application, because bootstrapper is still being called in Global.asax.

Is there a way that my mapping code works for my MVC application as well as for another application other than MVC (maybe WCF service, console application, etc.). Where can I put this mapping code so that it is called by two applications only once?

+4
source share
2 answers

Here is a static class that can be used to initialize WCF services:

public static class ServiceConfigurations { private static bool mappingConfigured = false; public static void ConfigureMapping() { if (mappingConfigured) { return; } Mapper.CreateMap<Model1, Model2>(); mappingConfigured = true; } public static void CleanupMapping() { Mapper.Reset(); mappingConfigured = false; } } 
+2
source

Why not just add the global.asax file to your services project?

Throw your mappings into a new project, a link in both, and you're done.

0
source

Source: https://habr.com/ru/post/1393198/


All Articles