简体   繁体   English

如何在 ASP.Net webapp 中的引用项目 DLL 中初始化 AutoMapper 配置文件

[英]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.

相关问题 Automapper无法在asp.net核心集成测试项目中初始化 - Automapper fails to initialize in asp.net core Integration Testing project 如何将不同的配置文件添加到asp.net项目? - How do I add different profiles to an asp.net project? 如何在ASP.NET项目中发布引用的项目 - How to publish referenced projects within an ASP.NET project .NET 中另一个项目使用的项目中引用的 DLL - DLLs referenced in a project used by another project in .NET 如果不是ASP.net应用程序,如何配置AutoMapper? - How to configure AutoMapper if not ASP.net Application? 如何从asp.net项目中删除包中的DLL(程序集)? - How to remove DLLs(Assembly) from package from asp.net project? 如何将第三方dll从bin文件夹移动到ASP.NET Web Forms项目中的另一个文件夹? - How to move third party dlls from bin folder to another folder in ASP.NET Web Forms project? 通过Asp.Net Core Dependency注入添加AutoMapper并注入配置文件 - Adding AutoMapper via Asp.Net Core Dependency injection and inject into profiles .NET部署项目不使用但引用的dll需要的dll - .NET deploying dlls that project does not use but referenced dlls require 搜索ASP.Net配置文件 - Search ASP.Net Profiles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM