[英]How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp
Struggling a little on how to use automapper in my project class libraries (dlls).关于如何在我的项目类库 (dll) 中使用 automapper 有点挣扎。 See my structure of my overall solution below.
在下面查看我的整体解决方案的结构。
The WebApp fires up, and in Global.asax App Start, the AutoMapper.Configure() method is called to add the mapping profiles. WebApp 启动,在 Global.asax App Start 中,调用 AutoMapper.Configure() 方法来添加映射配置文件。 For now I am just adding the Services.AutoMapperViewModelProfile.
现在我只是添加 Services.AutoMapperViewModelProfile。 But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below).
但是我需要以某种方式说明每个 WebStoreAdapters(下面示例中的 BigCommerce 和 Shopify)中的配置文件。 I was hoping not to add references to each WebStoreAdapter in WebApp, just for the sake of being able to add the profiles during the AutoMapperConfig.
我希望不要在 WebApp 中添加对每个 WebStoreAdapter 的引用,只是为了能够在 AutoMapperConfig 期间添加配置文件。 If I add another call to AutoMapper.Initialize in WebStoreFactory, it overrides the one in WebApp.
如果我在 WebStoreFactory 中添加另一个对 AutoMapper.Initialize 的调用,它会覆盖 WebApp 中的调用。
Is there another way that I am missing or totally off base here in some other way?是否有另一种方式让我以其他方式失踪或完全偏离基地?
WebApp
- AutoMapperConfig
- AddProfile Services.AutoMapperViewModelProfile
Services.dll
- AutoMapperViewModelProfile
Scheduler.dll (uses HangFire to execute cron jobs to get data from shop carts. Its UI is accessed via the WebApp)
WebStoreAdapter.dll
-WebStoreFactory
BigCommerceAdapter.dll
- AutoMapperBigCommerceDTOProfile
ShopifyAdapter.dll
- AutoMapperShopifyDTOProfile
Initializing as called from Global.asax:从 Global.asax 调用初始化:
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(am =>
{
am.AddProfile<AutoMapperViewModelProfile>();
});
}
}
Profile:简介:
public class AutoMapperViewModelProfile : Profile
{
public override string ProfileName
{
get { return this.GetType().ToString(); }
}
protected override void Configure()
{
CreateMap<InventoryContainerHeader, InventoryContainerLabelPrintZPLViewModel>()
.ForMember(vm => vm.StatusDescription, opt => opt.MapFrom(entity => entity.InventoryContainerStatus.DisplayText))
.ForMember(dest => dest.ContainerDetails, option => option.Ignore())
;
...
}
}
One way to do this is to use reflection to load up all profiles:一种方法是使用反射加载所有配置文件:
var assembliesToScan = AppDomain.CurrentDomain.GetAssemblies();
var allTypes = assembliesToScan.SelectMany(a => a.ExportedTypes).ToArray();
var profiles =
allTypes
.Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => !t.GetTypeInfo().IsAbstract);
Mapper.Initialize(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
You don't directly reference any one Automapper profile, but just load all Profile's from the current AppDomain.您不直接引用任何一个 Automapper 配置文件,而只是从当前 AppDomain 加载所有配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.