我正在尝试在应用程序级别以多对多关系管理一些简单的列表。teacher.AddStudent()
每次在它们之间建立连接(例如,student.RemoveTeacher()
)时,我都希望更新两个对象中的关系。
我知道如果我使用 SQL,我会使用类似StudentTeacher
table 的东西来管理关系,但我试图了解如何在应用程序中执行此操作。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace StudentTeacher
{
// create simple interface
public interface IPerson {}
// abstract class
public abstract class Person : IPerson
{
public int Id { get; set; }
}
// concrete "Teacher"
public class Teacher : Person
{
// private list of student ids
private List<int> _studentIds { get; set; }
// readonly accessor
public ReadOnlyCollection<int> StudentIds {
get { return _studentIds.AsReadOnly(); }
}
// constructor
public Teacher(int id) {
Id = id;
_studentIds = new List<int>();
}
// add a student
public void AddStudent(Student student)
{
if (!_studentIds.Contains(student.Id)) {
_studentIds.Add(student.Id);
student.AddTeacher(this);
}
}
// remove a student
public void RemoveStudent(Student student)
{
if (_studentIds.Contains(student.Id)) {
_studentIds.Remove(student.Id);
student.RemoveTeacher(this);
}
}
public string ToString() {
return String.Format("Id:{0} | StudentIds:{1}", Id, String.Join(",", StudentIds));
}
}
/// concrete "Student"
public class Student : Person
{
// private list of teacher ids
private List<int> _teacherIds { get; set; }
// readonly accessor
public ReadOnlyCollection<int> TeacherIds {
get { return _teacherIds.AsReadOnly(); }
}
// constructor
public Student(int id) {
Id = id;
_teacherIds = new List<int>();
}
// add a teacher
public void AddTeacher(Teacher teacher)
{
if (!_teacherIds.Contains(teacher.Id)) {
_teacherIds.Add(teacher.Id);
teacher.AddStudent(this);
}
}
// remove teacher
public void RemoveTeacher(Teacher teacher)
{
if (_teacherIds.Contains(teacher.Id)) {
_teacherIds.Remove(teacher.Id);
teacher.RemoveStudent(this);
}
}
// simple override
public string ToString() {
return String.Format("Id:{0} | TeacherIds:{1}", Id, String.Join(",", TeacherIds));
}
}
class Program
{
static void Main(string[] args)
{
// create teachers
var teacher1 = new Teacher(101);
var teacher2 = new Teacher(102);
// create students
var student1 = new Student(501);
var student2 = new Student(502);
var student3 = new Student(503);
// create some relationships
teacher1.AddStudent(student1);
teacher1.AddStudent(student2);
teacher1.AddStudent(student3);
// see what's happening
Console.WriteLine(teacher1.ToString()); // "Id:101 | StudentIds:501,502,503"
Console.WriteLine(student1.ToString()); // "Id:501 | TeacherIds:101"
Console.WriteLine(student2.ToString()); // "Id:502 | TeacherIds:101"
Console.WriteLine(student3.ToString()); // "Id:503 | TeacherIds:101"
// update student1, student2
student1.AddTeacher(teacher2);
student2.AddTeacher(teacher2);
// -- Outputs
Console.WriteLine(student1.ToString()); // "Id:501 | TeacherIds:101,102"
Console.WriteLine(student2.ToString()); // "Id:502 | TeacherIds:101,102"
Console.ReadLine();
}
}
}
对我来说,直接调用相反方向的添加/删除方法感觉不对;
if (!_studentIds.Contains(student.Id)) {
_studentIds.Add(student.Id);
student.AddTeacher(this); // calling this directly feels bad
}
我有一种感觉,这很快就会变得非常混乱。我的问题是;
- 我是否应该寻找添加诸如静态观察者之类的东西
StudentTeacherObserver
并在其上引发事件,例如OnTeacherAdded
,OnStudentAdded
? - 是否有管理此类关系的特定模式?
- 是否有适合此目的的更好的数据结构,如果有,它是什么以及如何使用它?