Boring intro:
I know – DDD isn’t about technology. As i see it – DDD is all about creating ubiquitous language with product owner and reflecting it into code in such a simple and structured manner, that it just can’t be misinterpreted or lost.
But here comes a paradox into play – in order to get rid of technical side of application in domain model, it gets kind a technical – at least from design perspective.
Last time i tried to follow DDD – it ended up with whole logic outside of domain objects into ‘magic’ services all around and anemic domain model.
I’ve learnt some new ninja tricks and wondering if I could handle Goliath this time.
Problem:
class store : aggregateRoot {
products;
addProduct(product){
if (new FreshSpecification.IsSatisfiedBy(product))
products.add(product);
}
}
class product : entity {
productType;
date producedOn;
}
class productTypeValidityTerm : aggregateRoot {
productType;
days;
}
FreshSpecification is supposed to specify if product does not smell. In order to do that – it should check type of product, find by it days how long product is fresh and compare it with producedOn. Kind a simple.
But here comes problem – productTypeValidityTerm and productType are supposed to be managed by client. He should be able to freely add/modify those. Because I can’t traverse from product to productTypeValidityTerm directly, i need to somehow query them by productType.
Previously – i would create something like ProductService that receives necessary repositories through constructor, queries terms, performs some additional voodoo and returns boolean (taking relevant logic further away from object itself and scattering it who knows where).
I thought that it might be acceptable to do something like this:
addProduct(product, productTypeValidityTermRepository){...}
But then again – i couldn’t compose specification from multiple specifications underneath freely what’s one of their main advantages.
So – the question is, where to do that? How store can be aware of terms?
With the risk of oversimplifying things: why not make the fact whether a
Productis fresh something a product “knows”? AStore(or any other kind of related object) should not have to know how to determine whether a product is still fresh; in other words, the fact that something likefreshSpecificationorproductTypeValidityTermeven exist should not be known toStore, it should simply checkProduct.IsFresh(or possibly some other name that aligns better with the real world, likeShouldbeSoldBy,ExpiresAfter, etc.). The product could then be aware how to actually retrieve theprotductTypeValidityTermby injecting the repository dependency.It sounds to me like you are externalizing behavior which should be intrinsic to your domain aggregates/entities, eventually leading (again) to an anemic domain model.
Of course, in a more complicated scenario, where freshness depends on context (e.g., what’s acceptable in a budget store is not deemed worthy for sale at a premium outlet) you’d need to externalize the entire behavior, both from product and from store, and create a different type altogether to model this particular behavior.
Added after comment
Something along these lines for the simple scenario I mentioned: make the
FreshSpecpart of the Product aggregate, which allows theProductRepository(constructor-injected here) to (lazy) load it when needed.The store doesn’t know about these internals: all it cares about is whether or not the product is fresh: