How to Configure AutoMapper Once Per AppDomain

后端 未结 4 1147
别跟我提以往
别跟我提以往 2020-12-24 07:36

My current project with assemblies for the domain model, MVC web application, and unit tests. How can I set up the AutoMapper configuration so that all assemblies reference

4条回答
  •  盖世英雄少女心
    2020-12-24 08:25

    What we do is create a static class, something like BootStrapper, and put the initialization code in a static method in there. We're doing profiles, so you don't see much in there. Global.asax will call that at startup, domain will use it (since the configuration is singleton), and unit tests that need it call the BootStrapper.Configure() in their setup.

    One final thing we do is keep a flag around on the bootstrapper, and set it to true when we configure. That way, configuration only executes once per AppDomain. That means once at startup of the global.asax (Application_Start), and once when we run unit tests.

    HTH

提交回复
热议问题