Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper in Service Layer

I have an MVC project where I am using AutoMapper to map my Entity Framework Entities to View Models. The code that defines the mappings is in a boostrapper class that is called automatically when the application starts (App_Start, Global.asax)

I am doing some refactoring of my code to put all of my business logic in a Service Layer because we need to implement a batch process that runs daily which is doing some of the same logic as the MVC app.

One of the problems I am running into is now I need to map my database entities to some domain objects in my service layer. I think everything would still work fine in the MVC application because the bootstrapper is still being called in Global.asax.

Is there a way I can have my mapping code work for both my MVC application and another non-MVC application (could be a WCF service, console app, etc.) Where can I put this mapping code so it would get called by both applications only once?

like image 813
Dismissile Avatar asked Nov 05 '22 07:11

Dismissile


1 Answers

Here is static class, could be used for WCF services initialization:

 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;
    }
}
like image 170
paramosh Avatar answered Nov 11 '22 16:11

paramosh