概述
AutoMapper 是一個物件-物件映射器,可以將一個物件映射到另一個物件,
用來解決一個看似復雜的問題,這種型別的代碼撰寫起來相當枯燥乏味,
官網地址:
http://automapper.org/
官方檔案:
https://docs.automapper.org/en/latest/
入門
AutoMapper支持使用靜態服務位置構造“自定義值決議器”,“自定義型別轉換器”和“值轉換器”的功能:
var configuration = new MapperConfiguration(cfg => { cfg.ConstructServicesUsing(ObjectFactory.GetInstance); cfg.CreateMap<Source, Destination>(); });
或動態服務位置,用于基于實體的容器(包括子容器/嵌套容器):
var mapper = new Mapper(configuration, childContainer.GetInstance); var dest = mapper.Map<Source, Destination>(new Source { Value = https://www.cnblogs.com/lyl6796910/p/15 });
您可以使用組態檔定義配置,然后,通過在啟動時呼叫IServiceCollection擴展方法AddAutoMapper,使AutoMapper知道這些概要檔案在哪些程式集中定義:
services.AddAutoMapper(profileAssembly1, profileAssembly2 /*, ...*/);
或標記型別:
services.AddAutoMapper(typeof(ProfileTypeFromAssembly1), typeof(ProfileTypeFromAssembly2) /*, ...*/);
現在,您可以在運行時將AutoMapper注入服務/控制器中:
public class EmployeesController { private readonly IMapper _mapper; public EmployeesController(IMapper mapper) => _mapper = mapper; // use _mapper.Map or _mapper.ProjectTo }
當然還有很多可擴展性,比如:
定制型別轉換器
有時,您需要完全控制從一種型別到另一種型別的轉換,通常,這是當一種型別看起來與另一種型別不一樣時,已經存在轉換函式,并且您希望從“松散”型別變為更強的型別,例如字串的源型別到Int32的目標型別,
例如,假設我們的源型別為:
public class Source { public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } }
但您想將其映射到:
public class Destination { public int Value1 { get; set; } public DateTime Value2 { get; set; } public Type Value3 { get; set; } }
如果我們嘗試按原樣映射這兩種型別,則AutoMapper會拋出例外(在映射時和配置檢查時),因為AutoMapper不知道從字串到int,DateTime或Type的任何映射,要為這些型別創建映射,我們必須提供一個自定義型別轉換器,并且我們可以通過三種方式:
void ConvertUsing(Func<TSource, TDestination> mappingFunction); void ConvertUsing(ITypeConverter<TSource, TDestination> converter); void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>;
第一個選項就是任何帶有源并回傳目的地的函式(也有多個多載),這適用于簡單的情況,但對于較大的情況則顯得笨拙,在更困難的情況下,我們可以創建一個自定義的ITypeConverter <TSource,TDestination>:
public interface ITypeConverter<in TSource, TDestination> { TDestination Convert(TSource source, TDestination destination, ResolutionContext context); }
并向AutoMapper提供一個自定義型別轉換器的實體,或者為型別提供AutoMapper將在運行時實體化的型別,我們上面的源/目標型別的映射配置將變為:
public void Example() { var configuration = new MapperConfiguration(cfg => { cfg.CreateMap<string, int>().ConvertUsing(s => Convert.ToInt32(s)); cfg.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter()); cfg.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>(); cfg.CreateMap<Source, Destination>(); }); configuration.AssertConfigurationIsValid(); var source = new Source { Value1 = "5", Value2 = "01/01/2000", Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination" }; Destination result = mapper.Map<Source, Destination>(source); result.Value3.ShouldEqual(typeof(Destination)); } public class DateTimeTypeConverter : ITypeConverter<string, DateTime> { public DateTime Convert(string source, DateTime destination, ResolutionContext context) { return System.Convert.ToDateTime(source); } } public class TypeTypeConverter : ITypeConverter<string, Type> { public Type Convert(string source, Type destination, ResolutionContext context) { return Assembly.GetExecutingAssembly().GetType(source); } }
在第一個映射中,從字串到Int32,我們僅使用內置的Convert.ToInt32函式(作為方法組提供),接下來的兩個使用自定義ITypeConverter實作,
自定義型別轉換器的真正強大之處在于,只要AutoMapper在任何映射型別上找到源/目標對,它們就可以使用,我們可以構建一組自定義型別轉換器,并在其上使用其他映射配置,而無需任何其他配置,在上面的示例中,我們不必再次指定string / int轉換,由于必須在型別成員級別配置自定義值決議器,因此自定義型別轉換器的作用域是全域的,
當然還有很多功能需要去實際專案中實作,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/262319.html
標籤:.NET Core
上一篇:C# NOPI 專案實戰(經典)(可下載專案原始碼)
下一篇:.NET Core Swagger 的分組使, 以及相同Action能被多個分組公用,同時加載出尚未分組的資料出來
