C#使用AutoMapper實(shí)現(xiàn)類映射詳解
寫(xiě)在前面
AutoMapper是一個(gè)用于.NET中簡(jiǎn)化類之間的映射的擴(kuò)展庫(kù);可以在執(zhí)行對(duì)象映射的過(guò)程,省去的繁瑣轉(zhuǎn)換代碼,實(shí)現(xiàn)了對(duì)DTO的快速裝配,有效的減少了代碼量。
通過(guò)NuGet安裝,AutoMapper, 由于本例用到了DI,所以需要順便安裝一下 AutoMapper.Extensions.Microsoft.DependencyInjection
代碼實(shí)現(xiàn)
using AutoMapper; using AutoMapper.Internal; using Microsoft.Extensions.DependencyInjection; IServiceCollection services = new ServiceCollection(); services.AddTransient<ISomeService>(sp => new FooService(5)); services.AddAutoMapper(typeof(Source)); var provider = services.BuildServiceProvider(); using (var scope = provider.CreateScope()) { var mapper = scope.ServiceProvider.GetRequiredService<IMapper>(); foreach (var typeMap in mapper.ConfigurationProvider.Internal().GetAllTypeMaps()) { Console.WriteLine($"{typeMap.SourceType.Name} -> {typeMap.DestinationType.Name}"); } foreach (var service in services) { Console.WriteLine(service.ServiceType + " - " + service.ImplementationType); } var dest = mapper.Map<Dest2>(new Source2()); Console.WriteLine(dest!.ResolvedValue); } Console.ReadKey(); public class Source { public int Id { get; set; } public string Name { get; set; } } public class Dest { public int ResolvedValue { get; set; } } public class Source2 { public string Name { get; set; } public int ResolvedValue { get; set; } } public class Dest2 { public int ResolvedValue { get; set; } } /// <summary> /// 映射表1 /// </summary> public class Profile1 : Profile { public Profile1() { CreateMap<Source, Dest>(); } } /// <summary> /// 映射表1 /// </summary> public class Profile2 : Profile { public Profile2() { CreateMap<Source2, Dest2>() .ForMember(d => d.ResolvedValue, opt => opt.MapFrom<DependencyResolver>()); } } public class DependencyResolver : IValueResolver<object, object, int> { private readonly ISomeService _service; public DependencyResolver(ISomeService service) { _service = service; } public int Resolve(object source, object destination, int destMember, ResolutionContext context) { return _service.Modify(destMember); } } public interface ISomeService { int Modify(int value); } public class FooService : ISomeService { private readonly int _value; public FooService(int value) { _value = value; } public int Modify(int value) => value + _value; }
調(diào)用示例
到此這篇關(guān)于C#使用AutoMapper實(shí)現(xiàn)類映射詳解的文章就介紹到這了,更多相關(guān)C# AutoMapper類映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)為TextEdit設(shè)置水印文字的方法,對(duì)C#程序設(shè)計(jì)人員來(lái)說(shuō)是一個(gè)很實(shí)用的技巧,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫(xiě)的16進(jìn)制Unicode的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫(xiě)的16進(jìn)制Unicode的方法,分析了轉(zhuǎn)換的技巧并以實(shí)例形式給出了具體的轉(zhuǎn)換方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12C#開(kāi)發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器
這篇文章介紹了C#開(kāi)發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Unity實(shí)現(xiàn)卡片循環(huán)滾動(dòng)效果的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)卡片循環(huán)滾動(dòng)的效果,文中的實(shí)現(xiàn)步驟講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12C#實(shí)現(xiàn)訪問(wèn)Web API Url提交數(shù)據(jù)并獲取處理結(jié)果
Web API 是 Web 服務(wù)器和 Web 瀏覽器之間的應(yīng)用程序處理接口,我們常見(jiàn)的模式是訪問(wèn) Web API Url 地址,并獲取 Json 、XML或其它指定格式的處理結(jié)果, 本文我們介紹了使用C#實(shí)現(xiàn)訪問(wèn)Web API Url提交數(shù)據(jù)并獲取處理結(jié)果,需要的朋友可以參考下2024-05-05C#在Entity Framework中實(shí)現(xiàn)事務(wù)回滾
這篇文章介紹了C#在Entity Framework中實(shí)現(xiàn)事務(wù)回滾的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08