How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

爷,独闯天下 提交于 2019-12-06 05:16:44

问题


Struggling a little on how to use automapper in my project class libraries (dlls). 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. For now I am just adding the Services.AutoMapperViewModelProfile. But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below). 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. If I add another call to AutoMapper.Initialize in WebStoreFactory, it overrides the one in 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:

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())
                ;
        ...
   }
}

回答1:


One way to do this is to use reflection to load up all profiles:

        var assembliesToScane = 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 profile, but just load all Profile's from the current AppDomain.



来源:https://stackoverflow.com/questions/38555702/how-to-initialize-automapper-profiles-in-referenced-project-dlls-in-asp-net-weba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!