Take the 2-minute tour ×

Since there isn't respositories for value objects. How can I load all value objects?

Suppose we are modeling a blog application and we have this classes:

  • Post (Entity)
  • Comment (Value object)
  • Tag (Value object)
  • PostsRespository (Respository)

I Know that when I save a new post, its tags are saving with it in the same table. But how could I load all tags of all posts. Has PostsRespository got a method to load all tags? I usually do it, but I want to know others opinions

share|improve this question

3 Answers 3

up vote 5 down vote accepted

I'm looking for a better solution for this question and I found this post:


This post explain very well why there is a lot of confusion with value objects and databases. Here you are phrase which liked me too much:

  • "Persistence is not an excuse to turn everything to entities."

Gojko Adzic, give us three alternatives to save our value objects.

share|improve this answer
Relational databases and ORMs often trick us into creating accidental entities jefclaes.be/2013/05/accidental-entities-you-dont-need-that.html –  JefClaes May 27 '13 at 18:56
thanks for the link –  yeraycaballero May 30 '13 at 10:41

I am currently working through a similar example. Once you need to uniquely refer to tags they are no long simple value objects and may continue to grow in complexity. I decided to make them their own entities and create a separate repository to retrieve them. In most scenarios they are loaded or saved with the post but when they are required alone the other repository is used.

I hope this helps.

EDIT: In part thanks to this post I decided to restructure my application slightly. You are right that I probably was incorrectly making tags an entity. I have since changed my application so that tags are just strings and the post repository handles all the storage requirements around tags. For operations that need posts the tags are loaded with them. For any operation that just requires tags or lists of tags the repository has methods for that.

share|improve this answer
Thanks for the answer. So you have an TagsRepository. Do you have a Tags table in database too? –  yeraycaballero Feb 17 '10 at 8:07
I actually have not decided on the database yet. I probably will not have a separate database and am strongly considering an object or document database solution like mongo or couch. –  smaclell Feb 17 '10 at 13:10

Here is my take in how I might solve this type of problem in the way that I'm currently practicing DDD.

If you are editing something that requires tags to be added and removed from such as a Post then tags may be entities but perhaps they could be value objects and are loaded and saved along with the post either way. I personally tend to favor value objects unless the object needs to be modified but I do realize that there is a difference between entity object modeled as read only "snapshots" and actual value objects that lack identity. The tricky part is that perhaps sometimes what you would normally think of as a key could be part of a value object as long as it is not used as identity in that context and I think tags fall into this category.

If you are editing the tags themselves then it is probably a separate bounded context or at least a separate aggregate in which tags are themselves are the aggregate root and persisted through a repository. Note that the entity class that represents tags in this context doesn't have to be the same entity class for tags used in Post aggregate.

If your listing available tags on the display for read only purposes such as to provide a selection list, then that is probably a list of value objects. These value objects can but don't have to be in the Domain Model since they are mainly about supporting the UI and not about the actual domain.

Please chime in if anybody has any thoughts on why my take on this might be wrong but this is the way I've been doing it.

share|improve this answer

Your Answer


