Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the risk of oversimplifying things: why not make the fact whether a <code>Product</code> is fresh something a product "knows"? A <code>Store</code> (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 <code>freshSpecification</code> or <code>productTypeValidityTerm</code> even exist should not be known to <code>Store</code>, it should simply check <code>Product.IsFresh</code> (or possibly some other name that aligns better with the real world, like <code>ShouldbeSoldBy</code>, <code>ExpiresAfter</code>, etc.). The product could then be aware how to actually retrieve the <code>protductTypeValidityTerm</code> by injecting the repository dependency.</p> <p>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. </p> <p>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.</p> <p><strong>Added after comment</strong></p> <p>Something along these lines for the simple scenario I mentioned: make the <code>FreshSpec</code> part of the Product aggregate, which allows the <code>ProductRepository</code> (constructor-injected here) to (lazy) load it when needed.</p> <pre><code>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); } } </code></pre> <p>The store doesn't know about these internals: all it cares about is whether or not the product is fresh:</p> <pre><code>public class Store { private List&lt;Product&gt; Products = new List&lt;Product&gt;(); public void AddProduct(Product product) { if (product.IsFresh()) { Products.Add(product); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload