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
Leave a Reply