5

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?

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • Where you are going to host your services? If in another application, you have to init mapping like in your MVC application. In general you can move mapping code to common library. – paramosh Jan 26 '12 at 15:59
  • Yes in another project. Is there a good way to ensure that the bootstrapper is called in the other project? If it's a WCF service, is there any sort of app_start for WCF applications? – Dismissile Jan 26 '12 at 16:04
  • Look, here is good answer to your question http://stackoverflow.com/questions/5903843/initialize-in-wcf-service-application-project Also some helpful solutions described here http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx – paramosh Jan 26 '12 at 16:11
  • @DerekBeattie yes I'm using Ninject – Dismissile Jan 26 '12 at 18:23

2 Answers2

2

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;
    }
}
paramosh
  • 2,258
  • 1
  • 15
  • 23
0

Why not just add a global.asax file into your services project?

Throw your mappings into a new project, reference in both, and you are done.

John Farrell
  • 24,673
  • 10
  • 77
  • 110