C# Use AutoMapper to map data model

When writing data to SQL Server, there is always a process about mapping model.
The process is a transformation, such as A model transforming B model.
If the model contains too many properties, the code could became very long.
So we can use a plugin, AutoMapper, to avoid that.

AutoMapper : https://docs.automapper.org/

The following is an example of implementation about converting PersonModel to PersonDto, which is a list convert.

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
using System;
using System.Collections.Generic;

using AutoMapper;

namespace AutoMapExample
{
public class Program
{
public class PersonModel
{
public string PersionalID { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
}
public class PersonDto
{
public int? Id { get; set; }
public string Pid { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
}
public static void Main()
{
var personlist = new List<PersonModel>
{
new PersonModel
{
PersionalID = "E111111111",
Name = "TOM",
Age = 20,
},
new PersonModel
{
PersionalID = "B111111111",
Name = "J.Cole",
Age = 26,
}
};

var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<PersonModel, PersonDto>()
.ForMember(dest => dest.Pid, opt => opt.MapFrom(src => src.PersionalID))
.ForMember(dest => dest.Id, opt => opt.Ignore());
});

// Check for missing mapped properties.
config.AssertConfigurationIsValid();

var mapper = config.CreateMapper();
var dtoList = mapper.Map<List<PersonModel>, List<PersonDto>>(personlist);

dtoList.ForEach(x =>
{
Console.WriteLine($"id:{x.Id},pid:{x.Pid},name:{x.Name},age:{x.Age}");
});
}
}
}

output

1
2
id:,pid:E111111111,name:TOM,age:20
id:,pid:B111111111,name:J.Cole,age:26

Because id was set to ignore, it’s null when the console prints them.

The following is the mapping of the different name properity. Due to the name of the properity is different so I have to define a rule to map, such as PersionalID => Pid.

1
ForMember(dest => dest.Pid, opt => opt.MapFrom(src => src.PersionalID))

The following is a check for missing mapped properties.

1
config.AssertConfigurationIsValid()