当前位置: 首页 > 编程语言 > C#

HowtoconfigureonceAutoMapperforeachAppDomainShare

时间:2023-04-10 22:54:45 C#

HowtoconfigureAutoMapperonceforeachAppDomain我目前的项目包括域模型、MVCweb应用程序和用于单元测试的程序集。如何设置AutoMapper配置以便所有程序集都引用相同的配置?我想我可以将项目添加到Global.asax中的Web应用程序,但如何在单元测试中使用它?另外,如果在Global.asax中配置,领域模型会选择地图吗?非常感谢,KevDog。我们所做的是创建一个静态类,比如BootStrapper,并将初始化代码放在静态方法中。我们正在做配置文件,所以你不会在那里看到太多。Global.asax将在启动时调用它,域将使用它(因为配置是单例),并且需要它的单元测试在其设置中调用BootStrapper.Configure()。我们要做的最后一件事是在引导程序上保留一个标志,并在配置时将其设置为true。这样,每个AppDomain只执行一次配置。这意味着一旦启动了global.asax(Application_Start),就会运行单元测试。HTH我也使用引导加载程序来处理这种启动任务。我实际上使用了一堆引导程序,因为我疯了。对于自动化,我们发现制作一些AutoMappingBuddy类并用属性装饰它们会更干净。然后我们用一些反射调用连接映射器(不便宜,但它们只在开始时触发一次)。在我们厌倦了在1200多行文件的第841行查找AutoMapper问题后找到了这个解决方案。我考虑过发布代码,但我真的不能称之为purdy。无论如何,它是:首先,AutoMappingBuddy的一个简单接口:publicinterfaceIAutoMappingBuddy{voidCreateMaps();其次,提供一些胶水的小属性:publicclassAutoMappingBuddyAttribute:Attribute{publicTypeMappingBuddy{get;私有集;}publicAutoMappingBuddyAttribute(TypemappingBuddyType){if(mappingBuddyType==null)thrownewArgumentNullException("mappingBuddyType");MappingBuddy=mappingBuddyType;}publicIAutoMappingBuddyCreateBuddy(){ConstructorInfoci=MappingBuddy.GetConstructor(;Typeif(0])==null){thrownewArgumentOutOfRangeException("mappingBuddyType",string.Format("{0}没有无参数构造函数。"));}objectobj=ci.Invoke(newobject[0]);将obj作为IAutoMappingBuddy返回;}}三、AutoMappingEngine。这就是魔法发生的地方:publicstaticclassAutoMappingEngine{publicstaticvoidCreateMappings(Assemblya){foreach(Typetina.GetTypes()){varamba=t.GetCustomAttributes(typeof(AutoMappingBuddyAttribute),true).OfType().FirstOrDefault();if(amba!=null&&!mappingDictionary.ContainsKey(amba.MappingBuddy)){mappingDictionary.Add(amba.MappingBuddy,amba.CreateBuddy());}}foreach(IAutoMappingBuddymappingBuddyinmappingDictionary.Values){mappingBuddy.CreateMaps();}}privatestaticDictionaryGetMappingDictionary(Assemblya){if(!assemblyMappings.ContainsKey(a)){assemblyMappings.Add(a,newDictionary());返回assemblyMappings[a];}privatestaticDictionary>assemblyMappings=newDictionary>();有点绕了一个小时,可能有更优雅的方式到达那里。我尝试了上面的代码,但无法正常工作。我对其进行了一些修改,如下所示。我认为剩下要做的就是通过Global.asax的Bootstrapper调用它。希望这可以帮助。使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Reflection;使用自动映射器;namespaceAutomapping{publicclassAutoMappingTypePairing{publicTypeSourceType{get;放;}publicTypeDestinationType{get;放;}}publicclassAutoMappingAttribute:Attribute{publicTypeSourceType{get;私有集;}publicAutoMappingAttribute(TypesourceType){if(sourceType==null)thrownewArgumentNullException("sourceType");来源类型=来源类型;}}publicstaticclassAutoMappingEngine{publicstaticvoidCreateMappings(Assemblya){IListautoMappingTypePairingList=newList();foreach(Typetina.GetTypes()){varamba=t.GetCustomAttributes(typeof(AutoMappingAttribute),true).OfType().FirstOrDefault();if(amba!=null){autoMappingTypePairingList.Add(newAutoMappingTypePairing{SourceType=amba.SourceType,DestinationType=t});}}foreach(AutoMappingTypePairingmappingPairinautoMappingTypePairingList){Mapper.CreateMap(mappingPair.SourceType,mappingPair.DestinationType);我像这样使用它来关联源到目标配对:[AutoMapping(typeof(Cms_Schema))]publicclassSchema:ISchema{publicInt32SchemaId{get;放;}publicStringSchemaName{get;放;}publicGuidApplicationId{get;放;然后自动创建映射,我这样做:AutoMappingEngine.CreateMappings(程序集);我一直在将AutoMapperCreateMap调用移动到我的视图模型旁边的类中,它们在其中实现IAutomapperRegistrar接口。我使用反射找到IAutoMapperRegistrar实现,创建一个实例并添加注册。这是接口:publicinterfaceIAutoMapperRegistrar{voidRegisterMaps();下面是接口的实现:publicclassEventLogRowMaps:IAutoMapperRegistrar{publicvoidRegisterMaps(){Mapper.CreateMap().ConstructUsing(he=>newEventLogRow(he.Id)).ForMember(m=>m.EventName,o=>o.MapFrom(e=>e.Description)).ForMember(m=>m.UserName,o=>o.MapFrom(e=>e.ExecutedBy.Username)).ForMember(m=>m.DateExecuted,o=>o.MapFrom(e=>string.Format("{0}",e.DateExecuted.ToShortDateString())));以下是我在Application_Start中执行注册的代码:foreach(在Assembly.GetAssembly(typeof(ISaveableModel)).GetTypes())中键入foundTypeIAutoMapperRegistrar))){varconstructor=foundType.GetConstructor(Type.EmptyTypes);if(constructor==null)thrownewArgumentException("我们假设所有IAutoMapperRegistrar类都有空构造函数。");((IAutoMapperRegistrar)constructor.Invoke(null)).RegisterMaps();我认为这是合适的,至少有点合乎逻辑;他们更多易于遵循这种方式在我使用巨大的引导程序方法进行数百次注册之前,这开始变得令人头疼。思考?以上是C#学习教程:HowtoconfigureonceAutoMapperforeachAppDomain。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处: