-5

This is the class

public class Student
    {
        public string Name { get; set; }
        public int Id { get; set; }
       public Faculty FacultyName { get; set; }
        public void hw()
        {
            System.Console.WriteLine("hw done by Student");
        }
        class many { }
    }

and another

public class class StudentDTO{

    public string Name { get; set; }
    public int Id { get; set; }
   public FacultyDTO FacultyName { get; set; }
}

For this i need A mapper method implementation can any one help please with out using AUTO MAPPER>

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54

1 Answers1

0

Without using automapper, you can create a static class to add extension methods to your classes, for example:

public static class StudentMapper
{
    public static StudentDTO ToDTO(this Student student)
    {
        return new StudentDTO
        {
            Id = student.Id,
            Name = student.Name,
            FacultyName = student.FacultyName.ToDTO()
        };
    }

    public static Student ToClass(this StudentDTO studentDTO)
    {
        return new Student
        {
            Id = studentDTO.Id,
            Name = studentDTO.Name,
            FacultyName = studentDTO.FacultyName.ToClass()
        };
    }
}

You need to do the same for every class and dto.

And finally, an usage example:

StudentDTO studentDTO;
Student student = studentDTO.ToClass();

Student student;
StudentDTO studentDTO = student.ToDTO();
Andres
  • 2,729
  • 5
  • 29
  • 60
  • can u tell using generics .u can find clear question here https://stackoverflow.com/questions/48435326/object-mapping-using-generic-class – Srikanth Rokkam Jan 25 '18 at 04:07