Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: SO tag. An Entity or value type?

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

EDIT:

Imagine, that you have to build SO website. How would you consider 'tag'?

like image 320
Bartek Szabat Avatar asked Mar 24 '09 20:03

Bartek Szabat


People also ask

What is the difference between entity and value object in DDD?

The main difference between entities and value objects lies in the way we compare their instances to each other. The concept of identifier equality refers to entities, whereas the concept of structural equality - to value objects. In other words, entities possess inherent identity while value objects don't.

What is an entity in DDD?

An object defined primarily by its identity is called an ENTITY. An ENTITY is anything that has continuity through a life cycle and distinctions independent of attributes that are important to the application's user.


3 Answers

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

like image 83
George Mauer Avatar answered Oct 26 '22 12:10

George Mauer


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);
}
like image 23
Dmitry Avatar answered Oct 26 '22 13:10

Dmitry


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

like image 40
kitsune Avatar answered Oct 26 '22 13:10

kitsune