C# 如何在ASP.Net webapp中的引用项目DLL中初始化自动映射配置文件
在如何在我的项目类库(DLL)中使用automapper方面有些困难。请参见下面我的总体解决方案结构 WebApp启动,在Global.asax App Start中,调用AutoMapper.Configure()方法来添加映射配置文件。现在我只是添加Services.AutoMapperViewModelProfile。但我需要以某种方式说明每个WebStoreAdapters(下面的例子中是BigCommerce和Shopify)中的配置文件。我不希望在WebApp中添加对每个WebStoreAdapter的引用,只是为了能够在AutoMapperConfig期间添加概要文件。如果我在WebStoreFactory中添加另一个对AutoMapper.Initialize的调用,它将覆盖WebApp中的调用 有没有其他方式让我在这里迷失或完全偏离了方向C# 如何在ASP.Net webapp中的引用项目DLL中初始化自动映射配置文件,c#,asp.net,automapper,automapper-4,C#,Asp.net,Automapper,Automapper 4,在如何在我的项目类库(DLL)中使用automapper方面有些困难。请参见下面我的总体解决方案结构 WebApp启动,在Global.asax App Start中,调用AutoMapper.Configure()方法来添加映射配置文件。现在我只是添加Services.AutoMapperViewModelProfile。但我需要以某种方式说明每个WebStoreAdapters(下面的例子中是BigCommerce和Shopify)中的配置文件。我不希望在WebApp中添加对每个WebSto
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
从Global.asax调用初始化:
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(am =>
{
am.AddProfile<AutoMapperViewModelProfile>();
});
}
}
公共静态类自动映射器配置
{
公共静态void Configure()
{
Mapper.Initialize(am=>
{
am.AddProfile();
});
}
}
简介:
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())
;
...
}
}
公共类AutoMapperViewModelProfile:Profile
{
公共重写字符串配置文件名
{
get{返回this.GetType().ToString();}
}
受保护的覆盖无效配置()
{
CreateMap()
.ForMember(vm=>vm.StatusDescription,opt=>opt.MapFrom(entity=>entity.InventoryContainerStatus.DisplayText))
.FormMember(dest=>dest.ContainerDetails,option=>option.Ignore())
;
...
}
}
一种方法是使用反射加载所有配置文件:
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);
}
});
您不直接引用任何一个Automapper配置文件,只需从当前AppDomain加载所有配置文件。Hi,Mapper.Initialize-code被标记为过时。。当前添加配置文件的方式是什么?