views:

154

answers:

1

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?

+2  A: 

With the risk of oversimplifying things: why not make the fact whether a Product is fresh something a product "knows"? A Store (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 like freshSpecification or productTypeValidityTerm even exist should not be known to Store, it should simply check Product.IsFresh (or possibly some other name that aligns better with the real world, like ShouldbeSoldBy, ExpiresAfter, etc.). The product could then be aware how to actually retrieve the protductTypeValidityTerm by 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 FreshSpec part of the Product aggregate, which allows the ProductRepository (constructor-injected here) to (lazy) load it when needed.

public class Product {
  public ProductType ProductType { get; set; }
  public DateTime ProducedOn { get; set; }
  private FreshSpecification FreshSpecification { get; set; }
  public Product(IProductRepository productRepository) { }

  public bool IsFresh() {
    return FreshSpecification
      .IsSatisfiedBy(ProductType, ProducedOn);
  }
}

The store doesn't know about these internals: all it cares about is whether or not the product is fresh:

public class Store {
  private List<Product> Products = new List<Product>();
  public void AddProduct(Product product) {
    if (product.IsFresh()) {
      Products.Add(product);
    }
  }
}
tijmenvdk
I thought about this too - that i'm just modeling this wrong. I would be happy to accept your answer if you could rewrite my pseudo-code to reflect your idea.
Arnis L.
@tijmenvdk So - it's fine to inject product repository into product object, right? Somewhere read that it should be avoided (on the other hand, it kind a seems completely reasonable).
Arnis L.
So - basically, entities could be compared a bit with those well known good old `ProductManager`s but with all technical side stripped off and more thoroughly and carefully modeled. Is that right?
Arnis L.
And another thing - for constructing aggregate roots, common pattern is to use abstract factory. Is that correct?
Arnis L.
tijmenvdk
I'm not trying to get it by book. I'm just looking for conventions that won't leak after a week. Forget about that analogy - it's unimportant. Just one last thing... Factory comment actually was about dependency injection. Using factory for new objects seems fine, but i'm afraid about technical side when loading it inside repository. In my case - NHibernate is supposed to be underneath. @Mark Seemann maybe you got some comments on this? :)
Arnis L.
Found this http://stackoverflow.com/questions/340461/dependency-injection-with-nhibernate-objects i'm just not yet completely sure if that's nice and clean.
Arnis L.