出于安全考慮,在后臺與前臺進(jìn)行數(shù)據(jù)傳輸時,往往不會直接傳輸實(shí)體模型,而是使用Dto(Data transfer object 數(shù)據(jù)傳輸對象),這樣在后臺往前臺傳遞數(shù)據(jù)時可以省略不必要的信息,只保留必要的信息,大大增強(qiáng)數(shù)據(jù)安全性。
下面給出兩個相互對應(yīng)的關(guān)系模型User、UserDto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class User { private const int NameMaxLength = 20 ; private const int PassWordMaxLength = 16 ; [Key] public long Id { get; } [MaxLength(NameMaxLength)] public string Name { get; set; } [MaxLength(PassWordMaxLength)] [DataType(DataType.Password)] public string PassWord { get; set; } } public class UserDto { private const int NameMaxLength = 20 ; private const int PassWordMaxLength = 16 ; [MaxLength(NameMaxLength)] public string Name { get; set; } [MaxLength(PassWordMaxLength)] public string PassWord { get; set; } } |
這里將 Id 定義為自增長主鍵,在注冊頁面,這個 Id 應(yīng)不可見,這個時候使用Dto的好處就體現(xiàn)出來了,這個時候,在存入數(shù)據(jù)庫時會涉及到 UserDto 往 User 的類型轉(zhuǎn)換,按照之前的經(jīng)驗(yàn),肯定可以按照下面這樣來寫:
1
2
|
user.Name=userDto.Name; user.PassWord=UserDto.PassWord; |
這樣的轉(zhuǎn)換固然可以,但是如果一個 User 對象足夠復(fù)雜,有十幾個甚至二十幾個屬性,這個時候這種寫法就會顯得十分笨拙。
這個時候我們就可以借助AutoMapper來幫助我們完成 UserDto 到 User 的轉(zhuǎn)換了。
首先安裝Nuget包
在 Tools - Nuget Package Manage - Package Manage Console 輸入
1
|
Install-Package AutoMapper |
安裝相應(yīng)的Nuget包。
根據(jù) Github 上給出的幫助文檔來看,有兩種方法可以創(chuàng)建映射,一種是靜態(tài)的 Initalize 一種是動態(tài)創(chuàng)建。
下面使用兩種不同的方法來進(jìn)行單元測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public void Using_Initlalize_Test() { UserDto dto = new UserDto { Name = "Niko" , PassWord = "1234" , }; Mapper.Initialize(ctx => ctx.CreateMap<UserDto, User>()); User user = Mapper.Map<UserDto, User>(dto); user.Name.ShouldBe( "Niko" ); user.PassWord.ShouldBe( "1234" ); user.Id.ToString().ShouldBe( "0" ); } public void Using_MapperConfiguration_Test() { var config = new MapperConfiguration(ctx => ctx.CreateMap<UserDto, User>()); var mapper = config.CreateMapper(); // var mapper = new Mapper(config); UserDto dto = new UserDto { Name = "Niko" , PassWord = "1234" , }; User user = mapper.Map<User>(dto); //User user = Mapper.Map<User>(dto); user.Name.ShouldBe( "Niko" ); user.PassWord.ShouldBe( "1234" ); user.Id.ToString().ShouldBe( "0" ); } |
這里使用到 Shouldly 斷言框架,具體用法參考官方文檔。
寫完規(guī)則之后 通常會調(diào)用 AssertConfigurationIsValid 方法,檢查規(guī)則是否完整
1
|
Mapper.AssertConfigurationIsValid(); |
兩種方法,單元測試均通過。這樣的話,借助 Automapper 處理復(fù)雜的對象映射,將大大簡化我們的代碼量。
為了更加便捷地使用 AutoMappper ,對AutoMapper進(jìn)行擴(kuò)展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public static class AutoMapperExtension { /// <summary> /// 對象到對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static T MapTo<T>( this object obj) { if (obj == null ) return default (T); Mapper.Initialize(ctx=>ctx.CreateMap(obj.GetType(),typeof(T))); return Mapper.Map<T>(obj); } /// <summary> /// 集合到集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static List<T> MapTo<T>( this IEnumerable obj ) { if (obj == null ) throw new ArgumentNullException(); Mapper.Initialize(ctx => ctx.CreateMap ( obj.GetType(), typeof(T))) ; return Mapper.Map<List<T>>(obj); } } |
使用上面的方法進(jìn)行單元測試:
1
2
3
4
5
6
7
8
9
10
11
12
|
public void testme() { UserDto dto = new UserDto { Name = "Niko" , PassWord = "1234" , }; User user=dto.MapTo<User>(); user.Name.ShouldBe( "Niko" ); user.PassWord.ShouldBe( "1234" ); user.Id.ToString().ShouldBe( "0" ); } |
總結(jié)
以上所述是小編給大家介紹的Automapper實(shí)現(xiàn)自動映射的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/NikoRanger/archive/2017/09/28/7605250.html