最近發現了一個比較有趣的東西 AutoMapper,主要將Model轉換為DTO,DTO更注重數據,對領域物件進行合理封裝,從而不會將領域物件的行為過度暴露給表現層。
先來看一點實例,兩個類別之前的對應。
先定義兩個類別Source與DTOSource:
public class Source { public int Id { get; set; } public string Content { get; set; } } public class DTOSource { public int Id { get; set; } public string Content { get; set; } }
Source與DTOSource欄位完全相同,來看看它兩個如何透過AutoMapper轉換,程式碼很簡單。
Mapper.Initialize(x=>{ x.CreateMap<Source,DTOSource>(); }); Source s = new Source{Id=1,Content="123"}; DTOSource dto = Mapper.Map<DTOSource>(s);
第一步建立Source到DTOSource之間的映射,初始化一個Source實例後,來看下執行結果:
1 Mapper.Initialize(x => { 2 x.CreateMap<Source, DTOSource>().ForMember(c=>c.Desc,q=> { 3 q.MapFrom(z => z.Content); 4 }); 5 });來看看運行結果如何;
##可以看到與之前的運行結果是相同的。
那麼如何映射兩個List,其實也很簡單,和上述程式碼幾乎可以說是無差別,只是在最後一步時,要做一點點的修改就可以了。如下面程式碼:
Mapper.Initialize(x => { x.CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q => { q.MapFrom(z => z.Content); }); }); s.Add(new Source { Id = 1, Content = "123" }); var dto = Mapper.Map<List<DTOSource>>(s);
可以看到除了最後一句程式碼,其它幾乎是完全相同的,只是在最後一句程式碼中,目標類型改成了List41b38b92674a5da99941ae683e26df45僅此而已。看下運行結果如何:
結果符合預期。
在實際的專案中,這樣的寫法肯定是不符合要求的,一般會做一個封裝,新建一個SourceProfile繼承自Profile:
1 public SourceProfile() 2 { 3 base.CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q => { 4 q.MapFrom(z => z.Content); 5 }); 6 }
所有映射關係都可以寫在這一個類別裡,只需要在程式初始化的時候呼叫一次就可以了:
1 Mapper.Initialize(x =>{ x.AddProfile<SourceProfile>(); });
部落客使用的AutoMapper版本6.1.1.0,因為AutoMapper在6.0版本時移除了Profile中的Configure,所以與6.0版本以下寫法有點不同,6.0以下版本寫法為:
public class SourceProfile : Profile { protected override void Configure() { CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q => { q.MapFrom(z => z.Content); }); } }
繼承Profile重寫其Configure即可,呼叫方式與上述沒有太大差異。 Mapper.Initialize中可新增一個或多個Profile。
在MVC專案的應用程式中,可以將Mapper.Initialize封裝到一個類別裡;
public static class AutoMapperForMvc { public static void Register() { Mapper.Initialize(x => { x.AddProfile<SourceProfile>(); }); } }
進而在MVC的Global中進一次註冊:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //注册 AutoMapperForMvc.Register(); } }
以上是C#中關於AutoMapper應用的實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!