开发者

How to configure AutoMapper if not ASP.net Application?

开发者 https://www.devze.com 2022-12-30 00:12 出处:网络
I\'m using AutoMapper in a number of projects within my solution. These projects may 开发者_如何转开发be deployed independantly, across multiple servers.

I'm using AutoMapper in a number of projects within my solution. These projects may 开发者_如何转开发be deployed independantly, across multiple servers.

In the documentation for AutoMapper it says:

If you're using the static Mapper method, configuration only needs to happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications.

Whilst some of the projects will be ASP.net - most of these are class libraries / windows services.

Where should I be configuring my mappings in this case?


The idea that it should only be required once per AppDomain stays the same, as far as I can tell. I always perform my mappings upon the initialization of the program itself. While I am not using AutoMapper I am using an IoC library (Windsor) which requires a mapping of sorts and this is done from my program.cs file. So when the application loads it performs the mapping and because the resolver is static and in a shared library it is available globally.

I don't know if this answers your question or not, but essentially every app has an entry point and if you need your mappings immediately after entry then the entry is the best place to put them.


I've elected to store my mappings in separate classes for each project so that they are reusable.

    protected void Application_Start()
    {
        RegisterMaps();
    }

    private void RegisterMaps()
    {
        WebAutoMapperSettings.Register();
        BusinessLogicAutoMapperSettings.Register();
    }

This way I can easily call BusinessLogicAutoMapperSettings.Register() if I were to reuse only my BusinessLogic dll in another application or webservice

0

精彩评论

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