我正在使用两个不同的聚合根:Post和Question。他们两个都有一个类别。
到目前为止,我已将其实现为共享实体(我不确定DDD中的设计是否正确)。
public class Post
{
public Guid Id { get; private set; }
public Category Category { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
}
public class Question
{
public Guid Id { get; private set; }
public Category Category { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
}
public class Category
{
public int Id { get; private set; }
public string Name { get; private set; }
public string Key { get; private set; }
}
注意:我知道我正在陷入原始的痴迷反模式,并且我计划将原始重构为ValueObjects。
在阅读了这篇文章DDD: Share entity with multiple aggregate roots之后,我想也许应该在ValueObject(具有多个字段)中转换Category。
从理论上讲,类别可以是具有其自身生命周期的实体,但现实是我并没有真正添加/删除/更新类别。
是否可以在DDD上使用共享实体?还是我宁愿使用ValueObject?
答案 0 :(得分:2)
让我们先处理一个汇总:发布
现在回答您的问题:
是否可以在DDD上使用共享实体?还是我宁愿使用ValueObject?
这取决于您对类别的处理方式。
场景1:
您的应用程序中有一个功能(或页面)来显示该类别的所有帖子。我会采用以下设计:
public class Category
{
public int Id { get; set; }
//this is my in-memory database. Use repository and service to adjust yours
public static List<Post> Posts;
public Category()
{
Posts = new List<Post>();
}
public void AddPost(Guid id, string title, string body)
{
var post = new Post(id, title, body, this.Id);
//saving the post into in-memory. Perhaps you can check some business logic inside Post entity
Posts.Add(post);
}
// You can retrieve all posts of a single category
public IEnumerable<Post> GetAllPosts()
{
return Posts.Where(x => x.CategoryId == this.Id);
}
}
public class Post
{
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
public int CategoryId { get; private set; }
public Post(Guid id)
{
Id = id;
}
public Post(Guid id, string title, string body, int categoryId)
{
//I prefer to pass guid into domain from external services.
//Using this way, your service will have the id to return to upper layers.
//Alternatively you can create new guid here on your own
Id = id;
Title = title;
Body = body;
CategoryId = categoryId;
}
// you can retrieve a post detail
public Post GetPost()
{
return Category.Posts.FirstOrDefault(x => x.Id == this.Id);
}
}
在这种情况下,我只能看到一个聚合根:类别。
方案2:
您拥有帖子页面,用户可以从那里查看详细信息。此外,每个帖子都有一个类别,该类别将显示在该详细页面的某个位置。您可以进行以下简单设计:
public class Post
{
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
public string CatKey { get; private set; }
public Post(Guid id)
{
Id = id;
}
public Post(Guid id, string title, string body, string catKey)
{
//I prefer to pass guid into domain from external services.
//Using this way, your service will have the id to return to upper layers.
//Alternatively you can create new guid here on your own
Id = id;
Title = title;
Body = body;
//I don't even bother with category id. This is a simple value object, you can store all of your categories
//into a hashtable of key-value
CatKey = catKey;
}
// you can retrieve a post detail
public Post GetPost()
{
//get your post detail from repo
}
}
希望您现在就可以做出决定。
答案 1 :(得分:1)
Entity vs ValueObject的主要问题是,是否需要对具有相同值的Category的两个实例进行不同的跟踪?经典示例是美元钞票-在大多数情况下,序列号(ID)无关紧要,一美元与另一美元(ValueObject)相同。不过,如果您的域正在收集稀有账单,那将会改变。
我怀疑您的情况并非如此,因为看来类别实际上只是由名称和键组成。如果帖子的类别发生变化,您是否需要跟踪以前的类别是什么?