开发者

Where should I put my CreateMap calls for AutoMapper?

开发者 https://www.devze.com 2023-01-04 04:12 出处:网络
I\'m creating WCF services that return data contract types by mapping Entity Framework types. What is the best place to put the Mapper.CreateM开发者_运维百科ap calls? Should I do it in each service an

I'm creating WCF services that return data contract types by mapping Entity Framework types. What is the best place to put the Mapper.CreateM开发者_运维百科ap calls? Should I do it in each service and only for that service, or should I do it on service startup?

Thoughts?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消