zoukankan      html  css  js  c++  java
  • 在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.

  • 相关阅读:
    虚拟机安装RHEL8.0.0
    给KVM添加新的磁盘
    RedHat7.4安装在个人电脑(笔记本)中安装遇到的问题总结
    shell编程-ssh免交互批量分发公钥脚本
    Error:Connection activation failed: No suitable device found for this connection 问题最新解决方案
    Linux下系统防火墙的发展历程和怎样学好防火墙(iptalbes和firewalld)
    Linux bash命令行常用快捷键(Xshell和secure CRT以及gnome-terminal)
    编写mysql多实例启动脚本
    RHEL7配置端口转发和地址伪装
    java中的事务,四大特性,并发造成的问题,隔离级别
  • 原文地址:https://www.cnblogs.com/CnKker/p/15160124.html
Copyright © 2011-2022 走看看