首頁 C# AutoMapper 自動映射
文章
Cancel

C# AutoMapper 自動映射

情境說明

安裝AutoMapper

Desktop View

1
NuGet\Install-Package AutoMapper -Version 10.0.0

Class的Property存在相同命名

兩個Class可能長這樣

Desktop View

不使用AutoMapper時,一般會這樣手動Mapping資料

Desktop View

Mapping方式

使用AutoMapper的Mapping方式

Desktop View

1
2
3
4
5
6
IEnumerable<AssetEquipment> oriLst = getOriAsset();
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<AssetEquipment, AssetEquipmentDto>()
);
IMapper mapper = config.CreateMapper();
IEnumerable<AssetEquipmentDto> result = mapper.Map<IEnumerable<AssetEquipmentDto>>(oriLst); // 轉換型別

Class的Property命名完全不同

兩個Class可能長這樣

假設有個AssetEquipment.cs 與OriAsset.cs ,其中有幾個欄位儲存相同的資料

Desktop View AssetEquipment.cs

1
2
3
4
5
6
7
8
9
10
 public class AssetEquipment
 {
    public string AssetId { get; set; }
    public string ItemName { get; set; }
    public string ItemDescription { get; set; }
    public string DepartmentId { get; set; }
    public string Department { get; set; }
    public string EmploeeID { get; set; }
    public string Emploee { get; set; }
 }

OriAsset.cs

1
2
3
4
5
6
7
8
9
10
public class OriAsset
{
    public string PERNR { get; set; }
    public string ANLN1 { get; set; }
    public string TXT50 { get; set; }
    public string AIBN1 { get; set; }
    public string PATH_HEAD { get; set; }
    public string PATH_ITEM { get; set; }
    public string ANLHTXT { get; set; }
}

Mapping方式

透過AutoMapper可以設定兩個Class之間的Mapping欄位

Desktop View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 class Program
{
    static void Main(string[] args)
    {
    IEnumerable<OriAsset> oriLst = getOriAsset();
    var config = new MapperConfiguration(cfg =>
    cfg.CreateMap<OriAsset, AssetEquipment>()
    .ForMember(x => x.AssetId, y => y.MapFrom(o => o.PERNR))
    .ForMember(x => x.ItemName, y => y.MapFrom(o => o.ANLN1))
    .ForMember(x=>x.ItemDescription, y => y.MapFrom(o => o.TXT50))
     );
        IMapper mapper = config.CreateMapper();
        IEnumerable<AssetEquipment> result = mapper.Map<IEnumerable<AssetEquipment>>(oriLst); // 轉換型別
    }
    public static IEnumerable<OriAsset> getOriAsset()
    {
        var temp = new List<OriAsset>()
        {
            new OriAsset{PERNR="1",ANLN1="2",TXT50="a" }
        };
        return temp;
    } 
}
本文由作者按照 CC BY 4.0 進行授權