(28)ASP.NET Core AutoMapper組件

1.什麼是AutoMapper?

AutoMapper是一個對象-對象映射器。對象-對象映射經過將一種類型的輸入對象轉換爲另外一種類型的輸出對象來工做。使AutoMapper變得有趣的是,它提供了一些有趣的約定,免去用戶不須要了解如何將類型A映射爲類型B。只要類型B遵循AutoMapper既定的約定,就須要幾乎零配置來映射兩個類型。映射代碼雖然比較無聊,可是AutoMapper爲咱們提供簡單的類型配置以及簡單的映射測試,而映射能夠在應用程序中的許多地方發生,但主要發生在層之間的邊界中,好比,UI /域層之間或服務/域層之間。一層的關注點一般與另外一層的關注點衝突,所以對象-對象映射致使分離的模型,其中每一層的關注點僅會影響該層中的類型。git

2.如何在Core上面使用AutoMapper組件?

先在Startup.ConfigureServices注入AutoMapper組件服務,而後在Startup.Configure上獲取AutoMapper服務配置擴展類建立對象-對象映射關係,爲了好統一管理代碼,能夠新建一個AutoMapperExtension靜態類,把如下代碼封裝一下:github

public static class AutoMapperExtension
{
    /// <summary>
    /// 新增自動映射服務
    /// </summary>
    /// <param name="service"></param>
    /// <returns></returns>
    public static IServiceCollection AddAutoMapper(this IServiceCollection services)
    {
        #region 方案一
        //註冊AutoMapper配置擴展類服務
        services.TryAddSingleton<MapperConfigurationExpression>();
        //註冊AutoMapper配置擴展類到AutoMapper配置服務去
        services.TryAddSingleton(serviceProvider =>
        {
            var mapperConfigurationExpression = serviceProvider.GetRequiredService<MapperConfigurationExpression>();
            var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression);
            mapperConfiguration.AssertConfigurationIsValid();
            return mapperConfiguration;
        });
        //注入IMapper接口DI服務
        services.TryAddSingleton(serviceProvider =>
        {
            var mapperConfiguration = serviceProvider.GetRequiredService<MapperConfiguration>();
            return mapperConfiguration.CreateMapper();
        });
        return services;
        #endregion
    }

    /// <summary>
    /// 使用自動映射配置擴展類
    /// </summary>
    /// <param name="applicationBuilder"></param>
    /// <returns></returns>
    public static IMapperConfigurationExpression UseAutoMapper(this IApplicationBuilder applicationBuilder)
    {
        //獲取已註冊服務AutoMapper配置擴展類
        return applicationBuilder.ApplicationServices.GetRequiredService<MapperConfigurationExpression>();
    }
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    ......
    //添加自動映射組件DI服務
    services.AddAutoMapper();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ......
    //註冊組件以後,建立映射對象
  var expression = app.UseAutoMapper();
    expression.CreateMap<Customer, CustomerDto>();
    expression.CreateMap<Address, AddressDto>();
}

由於IMapper接口已經在ConfigureServices方法注入DI服務了,因此無需再從新注入,只須要直接使用IMapper調用其方法就能夠:express

public class BlogsController : Controller
{
    private IMapper _iMapper { get; }
    public BlogsController(IMapper iMapper)
    {
        _iMapper = iMapper;
    }
    // GET: Blogs
    public async Task<IActionResult> Index()
    {
    //對象-對象數據傳輸
        var dto = _iMapper.Map<CustomerDto>(CustomerInitialize());
        ......
    }
    //手動賦值客戶對象數據
    private Customer CustomerInitialize()
    {
        var _customer = new Customer()
        {
            Id = 1,
            Name = "Eduardo Najera",
            Credit = 234.7m,
            Address = new Address() { City = "istanbul", Country = "turkey", Id = 1, Street = "istiklal cad." },
            HomeAddress = new Address() { City = "istanbul", Country = "turkey", Id = 2, Street = "istiklal cad." },
            WorkAddresses = new List<Address>()
            {
                new Address() {City = "istanbul", Country = "turkey", Id = 5, Street = "istiklal cad."},
                new Address() {City = "izmir", Country = "turkey", Id = 6, Street = "konak"}
            },
            Addresses = new List<Address>()
            {
                new Address() {City = "istanbul", Country = "turkey", Id = 3, Street = "istiklal cad."},
                new Address() {City = "izmir", Country = "turkey", Id = 4, Street = "konak"}
            }.ToArray()
        };
        return _customer;
    }
}

運行效果:app

3.若是更加靈活使用AutoMapper組件?

相信在第二章節時候,相信你們都會發現一個問題,若是生產場景業務愈來愈龐大,需建立對應業務對象也會愈來愈多,若是面對這樣的業務場景難道要在Configure方法裏面建立愈來愈多的映射關係嗎?例:async

var expression = app.UseAutoMapper();
expression.CreateMap<A, ADto>();
expression.CreateMap<B, BDto>();
expression.CreateMap<C, CDto>();
expression.CreateMap<D, DDto>();
......

很顯然這樣子是不可行的,這樣會致使後續代碼愈來愈多,難以維護。那麼如今讓咱們來解決這個問題。首先新建一個自動注入屬性的AutoInjectAttribute密封類,具體代碼以下:ide

public sealed class AutoInjectAttribute : Attribute
{
    public Type SourceType { get; }
    public Type TargetType { get; }
    public AutoInjectAttribute(Type sourceType, Type targetType)
    {
        SourceType = sourceType;
        TargetType = targetType;
    }
}

新增這個AutoInjectAttribute密封類,目的是聲明每一個DTO對象(數據傳輸對象)與對應數據源對象是傳輸關係,方便在Configure裏面自動註冊建立映射關係,例:測試

//聲明源對象,目標對象
[AutoInject(sourceType: typeof(Customer),targetType:typeof(CustomerDto))]
public class CustomerDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public AddressDto HomeAddress { get; set; }
    public AddressDto[] Addresses { get; set; }
    public List<AddressDto> WorkAddresses { get; set; }
    public string AddressCity { get; set; }
}

而後建立一個自動注入AutoInjectFactory工廠類,檢測運行中的程序集是否有AutoInjectAttribute屬性聲明,若是有則插入一個類型數據集中返回,目的是把全部聲明須要映射DTO對象跟數據源對象自動建立映射關係:ui

public class AutoInjectFactory
{
    public List<(Type, Type)> AddAssemblys
    {
        get
        {
            var assemblys =new List<Assembly>() { Assembly.GetExecutingAssembly() };
            List<(Type, Type)> ConvertList = new List<(Type, Type)>();
            foreach (var assembly in assemblys)
            {
                var atributes = assembly.GetTypes()
                    .Where(_type => _type.GetCustomAttribute<AutoInjectAttribute>() != null)
                    .Select(_type => _type.GetCustomAttribute<AutoInjectAttribute>());
                foreach (var atribute in atributes)
                {
                    ConvertList.Add((atribute.SourceType, atribute.TargetType));
                }
            }
            return ConvertList;
        }
    }
}

在第2小節AutoMapperExtension靜態類的AddAutoMapper方法內修改以下代碼:this

#region 方案二
//注入AutoMapper配置擴展類服務
services.TryAddSingleton<MapperConfigurationExpression>();
//注入自動注入工廠類服務
services.TryAddSingleton<AutoInjectFactory>();
//注入AutoMapper配置擴展類到AutoMapper配置服務去
services.TryAddSingleton(serviceProvider =>
{
    var mapperConfigurationExpression = serviceProvider.GetRequiredService<MapperConfigurationExpression>();
    //經過自動注入工廠類獲取聲明數據源對象與DTO對象自動建立映射關係
    var factory = serviceProvider.GetRequiredService<AutoInjectFactory>();
    foreach (var (sourceType, targetType) in factory.AddAssemblys)
    {
        mapperConfigurationExpression.CreateMap(sourceType, targetType);
    }
    var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression);
    mapperConfiguration.AssertConfigurationIsValid();
    return mapperConfiguration;
});
//注入IMapper接口DI服務
services.TryAddSingleton(serviceProvider =>
{
    var mapperConfiguration = serviceProvider.GetRequiredService<MapperConfiguration>();
    return mapperConfiguration.CreateMapper();
});
return services;
#endregion

再新增一個使用自動注入工廠類服務靜態方法:spa

/// <summary>
/// 使用自動注入工廠類
/// </summary>
/// <param name="applicationBuilder"></param>
public static void UseAutoInject(this IApplicationBuilder applicationBuilder)
{
   applicationBuilder.ApplicationServices.GetRequiredService<AutoInjectFactory>();
}

而後在Startup.ConfigureServices注入AutoMapper組件服務,而後在Startup.Configure上調用UseAutoInject靜態方法,具體代碼以下:

app.UseAutoInject();

運行效果:

從運行結果來看依然是這個酸爽!

參考文獻:
AutoMapper

相關文章
相關標籤/搜索