服务和存储库的域驱动设计问题 [英] Domain Driven Design Question on Services and Repositories

查看:78
本文介绍了服务和存储库的域驱动设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体User和Course。用户可以参加几门课程,从而使关系一对多。但是,许多学生可以选择一门课程,因此它与多对多关系密切。

I have two entities User and Course. A user can take several courses which makes the relationship one to many. But a single course can be taken by many students so it makes it many to many relationship.

现在,我需要为用户注册一门课程。我的用户实体具有:

Now, I need to register a course for a user. My user entity has:

 public void AddCourse(Course course)
        {
            if (CoursesAlreadyAdded(course))
            {
                AddBrokenRule(new BrokenRule() { PropertyName = course.ClassCode, Message = String.Format("Course with classCode = {0} already added", course.ClassCode) });
                return;
            }

            UserCourses.Add(new UserCourse() { UserId = this.UserId, CourseId = course.CourseId, Course= course, User = this});
        }

这些类是通过Linq to SQL生成的。 Linq to sql无法执行多对多关系,因此我必须自己处理。

The classes are generated through Linq to SQL. Linq to sql is not capable of performing many to many relationships so I have to handle it myself.

现在,问题是我将如何将信息发送到数据库。 UserRepository.Save(user)是否应负责保存课程。

Now, the question is that how will I send the information to the database. Should UserRepository.Save(user) be responsible for saving the courses.

这种方式意味着User是Course实体的聚合根,但实际上并不是这样,因为我可以通过许多不同的方式访问Course,而我并不依赖于User对象为我提供课程。

This kind of means that User is aggregate root of Course entity but in real it is not since I can access Course many different ways and I am not dependent on the User object to provide me Course.

即使我拥有CourseRegistrationService(我拥有),我也必须调用存储库以保留更改。哪个存储库负责保留有关用户和课程关系的更改。也许是UserCourseRepository!

Even if I have a CourseRegistrationService (which I have) I have to call a repository to persist the changes. Which repository is responsible for persisting the changes about user and course relationship. Maybe a UserCourseRepository!!

此外,只需在用户对象下放置一个列表,就可以使用户成为聚合根或不正确。如果是这样,那么您将如何使用OR MAPPERS设计应用程序,该应用程序会自动生成List和一对多关系。

Also, just by putting a List under User object makes User an aggregate root or is that incorrect. If so then how would you design application using OR MAPPERS which generates List and one to many relationships automatically.

推荐答案

用DDD术语来说,应该考虑骨料和骨料根。用户是否拥有课程?

In DDD terms you should be thinking of aggregates and aggregate roots. Does a User "own" the course? Probably not.

相反,这样想可能会为您提供更好的设计:

Instead, thinking of it like this might give you a better design:

用户有许多注册。
注册与1门课程相关。

现在您没有很多。您有一个User实体拥有的一流对象。

Now you don't have a many to many. You have a first class object that the User entity "owns".

注册将是一个值对象(具有UserID,CourseID以及DateAdded)。

Registration would be a value object (that has UserID, CourseID, and perhaps DateAdded).

关于使用方法添加到集合的两面,这也是我使用NHibernate所做的事情。

Regarding using methods to add to both sides of a collection, this is something that I do as well with NHibernate.

这篇关于服务和存储库的域驱动设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆