views:

39

answers:

1

I'm creating WCF services that return data contract types by mapping Entity Framework types. What is the best place to put the Mapper.CreateMap calls? Should I do it in each service and only for that service, or should I do it on service startup?

Thoughts?

A: 

I think you can create it once and cache in static field:

private static MapClass _MapInstance;

public static MapClass Map
{
    get
    {
        if(_MapInstance == null)
           _MapInstance = Mapper.CreateMap();
        return _MapInstance;
    }
}

Also as far as I know creating mapper is performance-expencive operation, because it may use code generation, sou you shouldn't do it on each call.

STO
Yep, CreateMap is expensive, as it does all the reflection optimization up-front. It only needs to be called once per AppDomain.
Jimmy Bogard