• 在autofac上注册automapper配置文件


    AutoMapper is simple library that takes care of Object-to-Object mappings which is otherwise boring & redundant to code every-time. An example scenario would be creating a Data Transfer Objects(DTOs) from a Data Model (Entity).

    The map configuration is usually done once per App domain so you would probably add it in the Application startup like global.asax. This would mean that you need to do a reference of all namespaces that contains mapping in your app startup file like below

    Mapper.Initialize(cfg => cfg.CreateMap<Com.Davidsekar.Order, Com.Davidsekar.OrderDto>());
    //or
    var config = new MapperConfiguration(cfg => cfg.CreateMap<Com.Davidsekar.Order, Com.Davidsekar.OrderDto>());

    So to overcome this type of concrete references,
    AutoMapper offers a way to group all map creation as an AutoMapper Profile within its  respective namespaces, which allows you to keep the map registration within its library.

    namespace Com.Davidsekar.Models.Mapping
    {
        using AutoMapper;
        using Com.Davidsekar.Models.Data;
        using Com.Davidsekar.Models.Dto;
    
        public class ContactFormMappingProfile : Profile
        {
            #region Constructors
    
            public ContactFormMappingProfile()
            {
                CreateMap<ContactForm, ContactFormDto>().ReverseMap();
            }
    
            #endregion Constructors
        }
    }

    You can keep the mappings in the AutoMapper profile class within library and then, these individual profiles can be easily scanned and initialized using Autofac using following code

    /// <summary>
    /// Registers the AutoMapper profile from the external assemblies.
    /// </summary>
    /// <param name="builder">The builder.</param>
    private static void RegisterMaps(ContainerBuilder builder)
    {
        var assemblyNames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
        var assembliesTypes = assemblyNames
            .Where(a => a.Name.Equals("Com.Davidsekar.Models", StringComparison.OrdinalIgnoreCase))
            .SelectMany(an => Assembly.Load(an).GetTypes())
            .Where(p => typeof(Profile).IsAssignableFrom(p) && p.IsPublic && !p.IsAbstract)
            .Distinct();
    
        var autoMapperProfiles = assembliesTypes
            .Select(p => (Profile)Activator.CreateInstance(p)).ToList();
    
        builder.Register(ctx => new MapperConfiguration(cfg =>
        {
            foreach (var profile in autoMapperProfiles)
            {
                cfg.AddProfile(profile);
            }
        }));
    
        builder.Register(ctx => ctx.Resolve<MapperConfiguration>().CreateMapper()).As<IMapper>().InstancePerLifetimeScope();
    }

    In above code, we are scanning through all referenced assemblies for an assembly with a particular name and  then, try to register all the types with type AutoMapper Profile. You can very well convert that single assembly name to a List<string> of assembly names.

    The above sample code give you a gist on how you can dynamically register all mapper configurations. Share your views.

  • 相关阅读:
    在javascript中如何取消事件冒泡
    ThinkPHP与EasyUI整合之二(datagrid):删除多条记录
    Jquery动画效果地铁站名指示等效果
    ubuntu 10.4 setup vm tools log
    Windows下Critical Section、Event、Mutex、Semaphores区别
    联通GPRS卡在windows mobile操作系统手机上网如何设置
    hope DATA
    电动车电池正确的使用方法
    C语言运算符表
    深圳市职业技能鉴定报名
  • 原文地址:https://www.cnblogs.com/CnKker/p/15160124.html
Copyright © 2020-2023  润新知