RSGC – DTO Mapper – part 6

 

 

name GeneratedMapper
nuget

https://www.nuget.org/packages/GeneratedMapper/

link https://github.com/ThomasBleijendaal/GeneratedMapper
author Thomas Bleijendaal

AutoMapping from a POCO to a DTO. Lots of customizations
 

The code that you start with is

01
02
03
04
05
06
07
08
09
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
public class Department                                   
 
    {
 
        public int ID { get; set; }
 
        public string Name { get; set; }
 
         
 
        public List<string> Employees { get; set; }
 
    }
 
 
 
    [IgnoreInTarget("Employees")]
 
    [MapFrom(typeof(Department))]
 
    public class DepartmentDTO
 
    {
 
        public int ID { get; set; }
 
        public string Name{get; set;}
 
 
 
        [MapWith("Employees",typeof(ResolverLength))]
 
        public int EmployeesNr { get; set; }
 
 
 
    }
 
    public class ResolverLength
 
    {
 
        public int Resolve(List<string> input)
 
        {
 
            return ((input?.Count) ?? 0);
 
        }
 
    }

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
static void Main(string[] args)                               
 
{
 
    var dep = new Department();
 
    dep.Name = "IT";
 
    dep.ID = 1;
 
    dep.Employees = new List<string>();
 
    dep.Employees.Add("Andrei");
 
    var dto = dep.MapToDepartmentDTO();
 
    Console.WriteLine(dto.Name+"=>"+ dto.EmployeesNr);
 
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
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
namespace DTOMapper                                                                                                                                                                                                       
 
{
 
    public static partial class DepartmentMapToExtensions
 
    {
 
        public static DTOMapper.DepartmentDTO MapToDepartmentDTO(this DTOMapper.Department self)
 
        {
 
            if (self is null)
 
            {
 
                throw new ArgumentNullException(nameof(self), "DTOMapper.Department -> DTOMapper.DepartmentDTO: Source is null.");
 
            }
 
             
 
            var resolverLength = new DTOMapper.ResolverLength();
 
             
 
            var target = new DTOMapper.DepartmentDTO
 
            {
 
                ID = self.ID,
 
                Name = (self.Name ?? throw new GeneratedMapper.Exceptions.PropertyNullException("DTOMapper.Department -> DTOMapper.DepartmentDTO: Property Name is null.")),
 
                EmployeesNr = resolverLength.Resolve((self.Employees ?? throw new GeneratedMapper.Exceptions.PropertyNullException("DTOMapper.Department -> DTOMapper.DepartmentDTO: Property Employees is null."))),
 
            };
 
             
 
            return target;
 
        }
 
    }
 
}

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DTOMapper

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators