0

I'm trying to determine if Automapper is a viable option for constructing parent/child object graphs from basic DTO collections, and I'm having trouble tracking down an example of the sort of approach I'm hoping for.

For example, I have the following DTO classes.

public class CourseDTO {

    public int CourseID { get; set; }
    public string CourseName { get; set; }

}

public class EnrolmentDTO {

    public int EnrolmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }

}

public class StudentDTO {

    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

I have a repository which returns collections of DTOs. E.g, a collection of courses in scope, a collection of enrolments for those courses, a collection of students for those enrolments.

Then I have this object graph class:

public class Course {

    public int CourseID { get; set; }
    public string CourseName { get; set; }
    public List<Enrolment> Enrolments { get; set; }

    public class Enrolment {

        public int EnrolmentID { get; set; }
        public int CourseID { get; set; }
        public int StudentID { get; set; }
        public Student Student { get; set; }

    }

    public class Student {

        public int StudentID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }

}

I want to be able to throw the DTO collections at an Automapper configuration and get back a collection of Course objects, with properties and child object properties populated. Is Automapper suitable for this and can anyone give me any pointers or a working example? Thanks.

Update for clarification:

What I'm trying to get Automapper to do is basically this:

Given a set of collections, e.g. list of CourseDTO, list of EnrolmentDTO and list of StudentDTO

First map the objects in the list of CourseDTO to a list of Course, the object graph 'root'

Then map the objects in the list of EnrolmentDTO into the object graph, matching on the CourseID value to ensure the enrolments related to the particular course become the mapped child objects.

Then map the objects in the list of StudentDTO into the Enrolment object graph, to ensure the student related to the enrolment is mapped.

Maff
  • 54
  • 4
  • Check this https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources – Dmitry Pavlov Nov 06 '17 at 15:39
  • Thanks Dmitry. This is similar to the examples I've seen already. The problem for me is that it's mapping just a couple of objects rather than a collection, and the two objects are known to be related before mapping. I've updated my question to clarify what I'm trying to do. – Maff Nov 06 '17 at 16:34
  • Maybe it would be a good idea to create class like AllCourseDataDTO with CourseDTO, list of EnrolmentDTO and list of StudentDTO and configure mapping from this object to Course? From where CourseDTO, EnrolmentDTO and StudentDTO come from? – Valerii Nov 06 '17 at 19:18

0 Answers0