chobo2
chobo2

Reputation: 85725

Where do I put this automapping?

HI

I have 2 projects one for my repos and service layers(data project) and one for my views,controllers(webUI project).

I am using automapper and I have been doing all my mapping in my controller. Say I had a request to get a item. It would go to my controller it would contact the service layer and any business logic would be done at this time.

I would get usually a domain model back and I would take that model and in the controller I would auto map it too a view model. Then send the View model back.

This has been working quite well as I been able to keep my mvc code(my viewModels and etc) out of my service layer.

However there have been 2 cases where I need to use automapper to do mapping in the service layer.

This mapping in the service layer is too other domain objects and has been quite small right now(only a few properties).

Should I be doing mapping in my service layer?

If so where do I stick these mappings? Right now I have it just in my project with my controllers that get registered in application start.

So one option could be to put my mapping where I been putting mapping(in a class in my models folder). The problem with this would be that if I take my service layer and pop it into another project(say a mobile device) then I have to redo all the mapping as it won't exist.

So any ideas?

Upvotes: 2

Views: 612

Answers (1)

John Farrell
John Farrell

Reputation: 24754

Have a class in your service layer called "ServiceLayerMappings" and call that from application start.

If you reuse the service layer just call ServiceLayerMappings.MapThisStuff() or whatever and you're all set.

public class ServiceLayerMappings
{
     public void Map()
     {

        Mapper.CreateMap<MyServiceClass, ServiceDto>();
     }
}

Global.asax.cs

    protected void Application_Start(object sender, EventArgs e)
    {
         new ServiceLayerMappings().Map()

You could of course make this static or rename. It doesn't matter.

Upvotes: 2

Related Questions