如何配置AutoMapper一旦每个AppDomain [英] How to Configure AutoMapper Once Per AppDomain

查看:132
本文介绍了如何配置AutoMapper一旦每个AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的项目,领域模型,MVC的Web应用程序,和单元测试组件。我怎样才能建立AutoMapper配置,使所有程序集引用相同的配置?

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 the same configuration?

我猜我可以把项目在Global.asax中的Web应用程序,但我该如何使用,在单元测试?此外,如果配置在Global.asax中,将域模型拿起地图吗?

I would guess that I could put items in Global.asax for the web app, but how can I use that in the unit tests? Also, if the config is in Global.asax, will the domain model pick up the map?

非常感谢,

KevDog。

推荐答案

我们要做的就是创建一个静态类,像引导程序,并把初始化code在静态方法在那里。我们正在做的配置文件,这样你就不会看到太多在那里。 Global.asax中会调用在启动时,域将使用它(因为配置单),并且需要调用BootStrapper.Configure()在其设置的单元测试。

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.

我们做的最后一件事是在引导程序保持一个标志周围,并将其设置为true,当我们配置。这样,配置只执行每个AppDomain一次。 ,在在Global.asax(的Application_Start)的启动意味着一旦,一旦当我们运行单元测试。

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.

心连心

这篇关于如何配置AutoMapper一旦每个AppDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆