DDD: SO tag. An Entity or value type?

后端 未结 3 1348
一整个雨季
一整个雨季 2020-12-19 00:56

In the context of Domain Driven Design, is a StackOverflow tag (ie. ffffd ) a value object or entity?

EDIT:

Imagine, that you have to build SO website. How wou

相关标签:
3条回答
  • 2020-12-19 01:31

    To expand a little on awhite's answer a tag is a value type Why? Because it doesn't make sense to have

    var tag1 = new Tag("DDD");
    var tag2 = new Tag("DDD");
    Assert.AreNotEqual(tag1, tag2);
    

    clearly they should be equal to each other because a tag has no identity except for its label. Questions and answers on the other hand are definitely entities

    0 讨论(0)
  • 2020-12-19 01:34

    SO tag is most likely an entity. Tags can be created, merged, deleted and renamed. There are features like 'similar tags', user's tags etc. Some of these functions, especially life cycle, will require an identity. Classic DDD example where Person that changes his/her name is still the same person, the same identity. The same with tags where user can decide to rename "domain-driven-design" to "DDD" and it will still be the same thing. Tags also need additional attributes like tag.Id, tag.Name, tag.CreatedOn, tag.CreatedBy, tag.Locked etc. There would probably be a corresponding tags repository that can enforce name uniqueness rule.

    To summarize, SO Tag is not a DDD Value Object because it is mutable and has a life cycle. More importantly, Tag is not only a characteristic of a Question (this is what I think was overlooked by other answers). It participates in a lot more relationships than that. In other words, Tag is more than just a sum of its attributes, it also has 'conceptual identity'. On the other hand TagName is a perfect example of Value Object. Its only purpose in life is to describe another entity (Tag). TagName is nothing more than just a string that may have a few built in rules like max length and case insensitive comparison. It may also make sense to simply use String instead.

    Code that displays questions may use something like this:

    IList<TagName> tags = question.GetTags();
    

    Code that tags the question can look like this:

    void TagQuestion(Question q, TagName tagName) {
        Tag tag = _tagsRepository.FindByName(tagName);
        if (tag == null) {
            tag = CreateNewTag( /* capture creator, date, other rules*/);
        }
        q.AddTag(tag);
    }
    
    0 讨论(0)
  • 2020-12-19 01:39

    Just some additional considerations: Tags can be normalized, "DDD" should be equal to "ffffd" and "DdD", and in most tag systems, spaces get replaced with "_" underscores. Also I guess the creator will be tracked for the badge system.

    0 讨论(0)
提交回复
热议问题