1
votes

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.

2
If it never changes, why not hard code the table as a global lookup? - Oded
So if I had more than one lookup of this type (eg. Meeting Length), but that had a different 'ID' type in the database, would I just have a Global lookup service, of which two methods are 'TimePeriod GetTimePeriods' and 'MeetingLength GetMeetingLengths'? - user676767
Could be - this definitely falls under the "service" umbrella, though I don't know if such domain specific methods are appropriate. - Oded
But the issue is that a Value (lookup) object may have ID of Guid or int, and one value object may have more fields than another, which makes creating a generic object (IValueObjectLookup) difficult and messy - otherwise this would be good to return from the service. (if return type is what you meant when talking about propriety - surely service per valueobject would be overkill). - user676767
A value object shouldn't have an ID. Its "identity" is defined by its value. - Oded

2 Answers

2
votes

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.

1
votes

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.