1

A design question which I need help with. It's best described with an example. Using the following Domain models: - Student - Enrollment - Course

Where Student and Course have a many to many relationship to each other, achieved by the Enrollment table, i.e. Enrollment has a StudentID (FK) and CourseID (FK). Both the Student and Course classes each have a Navigation Property, i.e. an ICollection of the other.

I'm using View models, and would like simple CRUD functionality, to add, edit, delete students and courses. The View models are very similar to their associated Domain models.

To display the student's details is simple enough, but when it comes to displaying the student's course details, which of the below designs would be the best approach?

  1. In the Student View model, declare an ICollection of the Enrollment Domain Model? Then in the view the enrollment details are accessible. I feel as if this undoes what the View model is trying to achieve, and that is to have an abstraction layer from the domain model. Using this design, the Enrollment Domain model is accessible from the View, via the Student View Model.

  2. Create a View model for the Enrollment class. This will be identical to it's Domain model. Doesn't do anything else other that hold the Domain model's values from the View Model. Has to be mapped via AutoMapper. Not sure what to make of this option, feel's inefficient.

OpcodePete
  • 887
  • 1
  • 14
  • 28

1 Answers1

2

First of all, Enrollment should not be a domain model. Enrollment is just a database table which specifies a many-to-many relationship from Students to Courses.

My suggestion is to create a List of Courses in the Student domain model, and use NHibernate or Fluent NHibernate to map the Student and Course, then create a many-to-many relationship from the mapping, and you can simply retrieve the Courses from a Student instance.

Also, you can use cascading operations more freely when using a mapping instead of writing some SQL statements in your code.

albusshin
  • 3,930
  • 3
  • 29
  • 57
  • Yes, good point, thanks. I see why I wouldn't need an Enrollment View model. Also, I'm using Entity Framework, and have now properly read up on Navigation Properties, and Fluent API. I see how you can create Foreign Key associations (via FKs in your classes) and Independent associations(via Navigation properties). – OpcodePete Dec 17 '13 at 03:13
  • If the Enrollment table contained a column (related to a Course record), would I then require the Enrollment View model? For example, the Course table contains static data, e.g. Course A, Course B... and I wanted to track if students where doing the Adult or Seniors version of the course, I would put a column CourseTypeAdultOrSenior in the Enrollment table. – OpcodePete Dec 17 '13 at 05:13
  • @ThomasVeil If this is the case, I would map the `Enrollment` table into a Domain Model as well. Please check [this answer](http://stackoverflow.com/a/5543702/1831275) to see how to do mappings in EF – albusshin Dec 17 '13 at 10:38