[Tools]AutoMapper

AutoMapper主要用來處理類別間的轉換
官方連結

簡單範例

  • 建立一個Model
1
2
3
4
5
6
7
8
9
10
11
namespace App.Domain.Model
{
public class MemberInfo
{
public int MemberID { get; set; }

public string NickName { get; set; }

public string Email { get; set; }
}
}
  • 建立對應的ViewModel,只需要顯示MemberID和NickName
1
2
3
4
5
6
7
8
9
namespace App
{
public class MemberInfoViewModel
{
public int MemberID { get; set; }

public string NickName { get; set; }
}
}
  • 一般類別轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void TransferClass()
{
MemberInfo infoModel = new MemberInfo()
{
MemberID = 123,
NickName = "abc",
Email = "abc@xxx.com"
};

MemberInfoViewModel infoViewModel = new MemberInfoViewModel
{
MemberID = infoModel.MemberID,
NickName = infoModel.NickName
};
}
  • AutoMapper寫法
1
2
3
4
5
6
7
8
9
10
11
12
13
private void TransferClass()
{
Mapper.CreateMap<MemberInfo, MemberInfoViewModel>();

MemberInfo infoModel = new MemberInfo()
{
MemberID = 123,
NickName = "abc",
Email = "abc@xxx.com"
};

MemberInfoViewModel infoViewModel = Mapper.Map<MemberInfoViewModel>(infoModel);
}
  • Linq轉換IEnumerable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void TransferClassList()
{
List<MemberInfo> infoModels = new List<MemberInfo>()
{
new MemberInfo
{
MemberID = 123,
NickName = "abc",
Email = "abc@xxx.com"
},
new MemberInfo
{
MemberID = 456,
NickName = "def",
Email = "def@xxx.com"
}
};

var infoViewModels = infoModels.Select(x => new MemberInfoViewModel
{
MemberID = x.MemberID,
NickName = x.NickName
});
}
  • AutoMapper轉換IEnumerable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void TransferClassList()
{
Mapper.CreateMap<IEnumerable<MemberInfo>, IEnumerable<MemberInfoViewModel>>();

List<MemberInfo> infoModels = new List<MemberInfo>()
{
new MemberInfo
{
MemberID = 123,
NickName = "abc",
Email = "abc@xxx.com"
},
new MemberInfo
{
MemberID = 456,
NickName = "def",
Email = "def@xxx.com"
}
};

var infoViewModels = Mapper.Map<IEnumerable<MemberInfoViewModel>>(infoModels);
}

AutoMapper全域設定 : 如果轉換有多個地方使用,可以寫在全域

  • 建立Model、ViewModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MemberInfoModel
{
public int MemberID { get; set; }

public string NickName { get; set; }

public string Email { get; set; }

public string ID { get; set; }

public string PW { get; set; }
}

public class MemberInfoViewModel
{
public int MemberID { get; set; }

public string NickName { get; set; }

public string UserId { get; set; }

public string Password { get; set; }
}
  • 在App_Start新增AutoMapperConfig
1
2
3
4
5
6
7
8
9
10
11
public class AutoMapperConfig
{
public static void Register()
{
Mapper.Initialize(cfg =>
{
cfg.AllowNullCollections = true;
cfg.AddProfile<MemberInfoProfile>();
});
}
}
  • 在App_Start新增MemberInfoProfile
1
2
3
4
5
6
7
8
9
public class MemberInfoProfile : Profile
{
public MemberInfoProfile()
{
CreateMap<MemberInfoModel, MemberInfoViewModel>()
.ForMember(dest => dest.Password, opt => opt.MapFrom(src => src.PW))
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.ID));
}
}
  • 在Global.asax呼叫
1
2
3
4
5
6
7
8
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AutoMapperConfig.Register();
}
}
  • 使用方式
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
30
31
32
33
34
// 單個
MemberInfoModel infoModel = new MemberInfoModel
{
Email = "aaa@xxx.com",
ID = "aaa",
MemberID = 1000001,
NickName = "aaa",
PW = "aaa123456"
};

var infoViewModel = Mapper.Map<MemberInfoViewModel>(infoModel);

// List
List<MemberInfoModel> infoModels = new List<MemberInfoModel>
{
new MemberInfoModel
{
Email = "aaa@xxx.com",
ID = "aaa",
MemberID = 1000001,
NickName = "aaa",
PW = "aaa123456"
},
new MemberInfoModel
{
Email = "bbb@xxx.com",
ID = "bbb",
MemberID = 1000001,
NickName = "bbb",
PW = "bbb123456"
}
};

var infoViewModels = Mapper.Map<IEnumerable<MemberInfoViewModel>>(infoModels);

轉換設定方式

  • Ignore: 略過
  • MapFrom: 指定對應屬性
  • ResolveUsing: 指定自訂的轉換方式
  • UseValue: 直接給值
  • NullSubstitute: 原始值為空才給值
  • Condition: 條件為True才給值
-------------The End-------------