Question

I have need of a TimePeriod (AM, PM, NIGHT, etc) reference in a number of Objects. eg. ScheduledMeeting, ActualMeeting.

Although TimePeriod is in the database with an ID, it is definitely not an entity, as it will not be changing, and a 'AM' period is the same across all objects.

My difficulty is in knowing how to retrieve this value object from the database. Since differing objects can reference it, it should not be part of any of their repository methods, but at the same time it shouldn't really have it's own repository, since it's not a root.

How then would you recommend fetching this from the database?

Thanks.

UPDATE

I have gone with eulerfx's answer, since he has explained that the periods can still return from the DB through the repository, although I don't really like the idea that you would be setting up multiple small repositories for multiple global value objects. This area does still feel a little grey to me.

Was it helpful?

Solution

Since TimePeriod is a value object, when other objects reference it, they reference the value itself not the ID. Make the time period object immutable so that once an object references a time period, it knows it won't change. Time period values can be stored in a database table and then the table records have IDs, however those don't need to be expressed in the code. You can have a simple repository for retrieving time periods, if for example you wish to provide a UI that displays a list of available time periods. A repository doesn't always have to return only aggregates. If time periods are highly static in nature then you can consider caching in memory, or even having a static class with all available time periods.

OTHER TIPS

Although TimePeriod is in the database with an ID, it is definitely not an entity, as it will not be changing.

As long as an object has a well-defined life-cycle, it can be an entity even if its contents don't change. The question to ask is "Will TimePeriods be created and deleted dynamically?"

  • Yes? Then TimePeriod should be an entity with its own repository. This is an example of the knowledge level that Eric Evans describes in his DDD book.

  • No? Then you don't need a database, just define the TimePeriods statically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top