C# AutoMapper 一個類別轉換到另一個類別的使用筆記

 
需安裝AutoMapper Package
PM> Install-Package AutoMapper




程式碼

 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using AutoMapper;

namespace AutoMapApp1
{
    public class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public string Department { get; set; }
    }

    public class EmployeeDTO
    {
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public string Department { get; set; }
    }

    public class EmployeeViewModel
    {
        public string FullName { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public string Dept { get; set; }
    }

    class Program
    {
        //Ref:
        //https://dotnettutorials.net/lesson/automapper-in-c-sharp/
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            Employee emp = new Employee();
            emp.Name = "Mike";
            emp.Salary = 35000;
            emp.Address = "Taipei";
            emp.Department = "IT";

            EmployeeDTO employeeDTO = new EmployeeDTO();
            employeeDTO.Name = emp.Name;
            employeeDTO.Salary = emp.Salary;
            employeeDTO.Address = emp.Address;
            employeeDTO.Department = emp.Department;
            Console.WriteLine("Name:" + employeeDTO.Name + ", Salary:" + employeeDTO.Salary + ", Address:" + employeeDTO.Address + ", Department:" + employeeDTO.Department);

            //CreateMap<TSource, TDestination>
            var config = new MapperConfiguration(cfg => cfg.CreateMap<Employee, EmployeeDTO>());
            var mapper = new Mapper(config);

            //TDestination Map<TSource, TDestination>(TSource source)
            var empDTO1 = mapper.Map<EmployeeDTO>(emp);
            Console.WriteLine("Name:" + empDTO1.Name + ", Salary:" + empDTO1.Salary + ", Address:" + empDTO1.Address + ", Department:" + empDTO1.Department);

            var empDTO2 = mapper.Map<Employee, EmployeeDTO>(emp);
            Console.WriteLine("Name:" + empDTO2.Name + ", Salary:" + empDTO2.Salary + ", Address:" + empDTO2.Address + ", Department:" + empDTO2.Department);

            var config2 = new MapperConfiguration(cfg => cfg.CreateMap<Employee, EmployeeViewModel>()
                                                        .ForMember(dst => dst.FullName, act => act.MapFrom(src => src.Name))
                                                        .ForMember(dst => dst.Dept , act => act.MapFrom(src => src.Department))
                                                        );
            Employee emp2 = new Employee();
            emp2.Name = "Sam";
            emp2.Salary = 38000;
            emp2.Address = "Hsinchu";
            emp2.Department = "PM";

            var mapper2 = new Mapper(config2);
            var emp2ViewModel = mapper2.Map<EmployeeViewModel>(emp2);
            Console.WriteLine("Name:" + emp2ViewModel.FullName + ", Salary:" + emp2ViewModel.Salary + ", Address:" + emp2ViewModel.Address + ", Department:" + emp2ViewModel.Dept);


            Console.ReadKey();
        }
    }
}



Ref:
https://dotnettutorials.net/lesson/automapper-in-c-sharp/
https://igouist.github.io/post/2020/07/automapper/
https://blog.csdn.net/catshitone/article/details/109840926
https://ithelp.ithome.com.tw/articles/10157130

留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題