c# - Reusing common mapping code across common DTO classes -


I have 2 DTO classes which are more than one common property, I avoid repeating myself when writing the mapping code I'm trying to get the unit for DTO conversion, I'm wondering how can I get it, I need to realize that I might have probably Func or verb Representatives will have to use to get this. For example, I have 2 classes StudentDTO and EmployeeDTO :

  Public class StudentDTO: PersonDTO {public int courseId {get; Set; } // other property} public class employees: DDTO {public entity business} {receive; Set; } // other properties}   

and individually derived from the DTO:

  public class PersonDTO {Public Ent ID (Receive; Set; } Public String First Name {Received; Set;} Public String Family Name {get; set}} Public int Age {Received; Set;}}   

How do I re-use the mapping code Can I map the common attributes? Thanks.

You can do something like this ( Not always basic and elegant): (Note that the unit can be closed course to be a datararider, dataset etc)

  public class unit {public string first name {get; set;} public string family Name {get; set;} Public Pros IID {Received; Set;} Public Full Business Professionals {Received; Set;}} Public Class Besadata {} Public Category PersonDto: BaseDto {Public String First Name {get; Set;} Public string family name {get; set;} {personDto.FirstName = entity.FirstName;} for public static zero map (entity unit, person person); Dot the person Family name = organization.familynames; }} Public class StudentDto: {Public int course ID for the person {get; Set; } Public static student map (unit unit) {var studentDto = new scholarship {courseId = entity.CourseId}; // .. If you want to return the student, call the person on the map; }} Public Class Employed Dots: Personal To {Public Ink Business} {Receive; Set; } Public stable employee map (institution unit) {var employee dots = new employee dot () {occupationId = entity.OccupationId}; // If you want to return the employee, call the person on the map; }} Public Square Mapper & lt; TDto & gt; Where TDto: BaseDto {Personal TDto _dto; The importance of a private readonly unit; Public Mapper (unit unit) {_entity = entity; } Public Mapper & lt; TDto & gt; Map (Funk & lt; Unit, TDto & gt; Map) {_dto = map (_entity); Back it; } Public Mapper & lt; TDto & gt; Maps & lt; TBaseDto & gt; (Action & lt; unit, TBaseDto & gt; Map) where TBaseDto: BaseDto {map (_Netity, _dto as TBaseDto); Back it; } Public TDto result {get {return _dto; }}} Class program {static zero main (string [] args) {var studentEntity = new Entity () {FirstName = "John", FamilyName = "Doe", CourseId = 1}; Var studentDto = new mapper & lt; StudentDto & gt; (Student Antitry). Map (StudentDto.Map). Map & lt; PersonDto & gt; (PersonDto.Map). result; }}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -