Reputation: 47
I'm implementing an entity UserImages with methods like addImage($imageId), removeImage($imageId), getImages($from, $count).
Phisycally, the data (a collection of image ids) stored in an application-level storage, which provides nice functionality like addItem($keyName, $item, $weight), removeItem($keyName, $item), getItems ($key, $from, $count).
How to make model use this external (looking form the domain) storage in DDD-style, without referencing the storage from the UserImages entity? Important that I don't want to load all the collection from the storage to the entity, as a traditional approach suppose.
Hope I provided a good explanation of the problem, please do let me know if not. Thank you very much for help.
Upvotes: 0
Views: 500
Reputation: 37729
UserImages
does not sound like an entity, but more like a service or repository, which you already have an implementation for - the application-level storage. You may wish to expose this as a UserImageRepository
, a repository being a more fitting name for what you have. More generally, whenever you have an association one end of which may have very large cardinality, consider implementing this association as a repository instead of a direct object reference. Entities and aggregates should be consistency boundaries, not necessarily entire incarnations of concepts they represent. Also, take a look at Effective Aggregate Design to go in-depth on this topic.
Upvotes: 6
Reputation: 7210
First, lazy loading is an antipattern in DDD, you need it if and only if entities provide access to more data than required to keep their invariants. To address this, you could use shared identifiers instead.
To decouple domain logic and persistency concerns you can use observable entities: the repository that provides the entity, keeps observing it so that, when proper domain events occurs, it can persist changes. However if you are coding in PHP, you have to code the Observer Pattern manually.
Upvotes: 0