存储库模式:存储库可以使用其他存储库吗?

发布于 2024-10-17 11:03:19 字数 456 浏览 0 评论 0原文

假设我有一个 TeacherRepository,需要根据下面的代码使用 CourseRepository。教师和课程形成多对多的关系。教师和课程不构成一个集合。您认为这种模式的正确使用吗?

class TeacherRepository {

    @Inject(@Named("courseRepository"))
    private final CourseRepository courseRepository;

    public void addCourseToTeachers(String courseName) {

      Course course = courseRepository.findByName(courseName);

      for (Teacher teacher : readAll()) 
        teacher.addCourse(course);
    } 
}

Suppose I have a TeacherRepository that needs to make use of a CourseRepository per the code below. Teacher and Course form a many to many relationship. Teacher and Course do not form an aggregate. Would you consider this proper use of the pattern?

class TeacherRepository {

    @Inject(@Named("courseRepository"))
    private final CourseRepository courseRepository;

    public void addCourseToTeachers(String courseName) {

      Course course = courseRepository.findByName(courseName);

      for (Teacher teacher : readAll()) 
        teacher.addCourse(course);
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮颜 2024-10-24 11:03:19

我不认为处理课程是 TeacherRepository 的任务。恕我直言,最好在一个单独的类中处理这个问题。最好为每个类保留单一职责。

更新

但是,如果您绝对想将此功能添加到 TeacherRepository 中,则可以在不依赖于 CourseRepository 的情况下完成此操作:

class TeacherRepository {
  public void addCourseToTeachers(Course course) {

    for (Teacher teacher : readAll()) 
      teacher.addCourse(course);
  } 
}

...
CourseRepository courseRepository = ...;
TeacherRepository teacherRepository = ...;
...
Course course = courseRepository.findByName(courseName);

if (course != null)
  teacherRepository.addCourseToTeachers(course);

I don't think it is the task of the TeacherRepository to deal with courses. IMHO it would be better to handle this in a separate class. It is better to keep a single responsibility for every class.

Update

But if you absolutely want to add this functionality to TeacherRepository, you can do it without any dependency to CourseRepository:

class TeacherRepository {
  public void addCourseToTeachers(Course course) {

    for (Teacher teacher : readAll()) 
      teacher.addCourse(course);
  } 
}

...
CourseRepository courseRepository = ...;
TeacherRepository teacherRepository = ...;
...
Course course = courseRepository.findByName(courseName);

if (course != null)
  teacherRepository.addCourseToTeachers(course);
神经暖 2024-10-24 11:03:19

我正在一个项目上做类似的事情,我没有看到你的方法有问题(并且不一定认为它与已发布答案的其他人的观点相矛盾)。

如果我有一个聚合根(教师),作为其聚合的一部分引用另一个聚合(课程),我认为该方法是有效的,原因如下:

  1. 每个存储库仍然负责其自己的聚合根的 CRUD 操作
  2. 它仍然是可能的通过每个存储库的 API 创建每个聚合的实例
  3. 教师存储库仍然有一项职责(创建教师聚合)。如果教师不包含对所教授课程的引用,您就无法创建教师。

我在这里没有看到这个问题,尽管随着我正在从事的项目的展开,我可能会获得更好的理解!

杰爱

I'm doing something similar on a project and I don't see the problem in your approach (and don't necessarily believe it contradicts the views of others who have posted answers).

If I have one Aggregate Root (Teacher) which as part of it's aggregate references another aggregate (Course), I think the approach is valid for the following reasons:

  1. Each repository is still responsible for CRUD operations of it's own aggregate root
  2. It is still possible to create instances of the each Aggregate through the API of each repository
  3. The Teacher repository still has one responsibility (To create a Teacher aggregate). It just happens that you cannot create a Teacher without the Teacher containing a reference to the taught courses.

I don't see the issue here although I may gain a better understanding as the project I'm working on unfolds!!.

JLove

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文