First of all I am making it clear that, I am not using any OR Mapper framework to solve this problem, coz I need to understand the model from the ground up.
OK. Let’s see the table designs:
Course – table
------------------
ID | CourseName
------------------
1 | C++
2 | Java
------------------
Teacher – table
-----------------------
ID | TeacherName
-----------------------
1 | Professor X
2 | Professor Y
-----------------------
CourseTeacher – table
---------------------------------------------
ID | CourseID | TeacherID | ClassTime
---------------------------------------------
1 | 1 | 1 | Monday 10:55
2 | 1 | 2 | Thursday 10:55
3 | 2 | 1 | Tuesday 11:45
4 | 2 | 2 | Wednesday 11:45
---------------------------------------------
How should I design my classes in C#?
There can be another problem like is:
User – table
------------------
ID | UserName
------------------
1 | a
2 | b
------------------
Role – table
-----------------------
ID | RoleName
-----------------------
1 | c
2 | d
-----------------------
UserRole – table
---------------------------------------------
ID | UserID | RoleID | Remarks
---------------------------------------------
1 | 1 | 1 | xy
2 | 1 | 2 | yz
3 | 2 | 1 | zx
4 | 2 | 2 | xx
---------------------------------------------
In general, when you have a many-to-many relationship in your domain model, the access patterns in your code will either be from one side of the M2M or from the other…
For e.g. If you have a structure containing people, each of whom has made a number of purchases at your retail outlets, you might as a rule want to access those transactions thrpugh a person, not through the product. in other words, you might more often want to know what products a specific person purchased, not what people purchased a specific product. So in this example you would simply have each person class contain a property which is a collection of products. There would be no need to access the person class through the Product class.
Another e.g. If you have a a structure containing people, each of whom subscribes to a number of magazines.
Now if you are the publisher, you might as a rule want to access those transactions through the magazine, not through the person. in other words, you would much more often want to know what people subscribe to a specific magazine, not what magazines a specific person subscribes to. So in this example you would put a Subscribers collection property in the Magazine class.