Reputation:
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?
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.
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.
Upvotes: 1
Views: 350
Reputation: 4010
First of all, Enrollment
should not be a domain model. Enrollment
is just a database table which specifies a many-to-many relationship from Student
s to Course
s.
My suggestion is to create a List
of Course
s 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 Course
s 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.
Upvotes: 2