title: Set of dynamic properties in domain-driven design
body: <p>After through search i was unable to find any question which answers this, in my opinion fairly common design problem.</p>
  
  <p>Given domain object:</p>
  
  <pre><code>public class Item {
      private Long itemSN;
      private String name;
  
      methods, etc...
  }
  </code></pre>
  
  <p>We need to store specific set of String properties which describes an item. It can be weight, color, sizes etc. System must be flexible and able to persist changeable list o properties. It needs to store allowed properties names, and preferably enforce some of them.</p>
  
  <p>I tried several approaches, but concept of common constraints shared by all Item objects just don't fit in any standard domain model.</p>
  
  <p>So i started to think about constraints as a form of configuration. Each Item has its's own properties (in simple String Map), constraints at the other hand are common configuration for all Items. So the next dilema emerged... how to express it without making big hole in domain model ? </p>
  
  <p>It's easy to introduce additional application layer object to store constraints, but "allowed/required properites" are business affair, we need to allow domain user (manager of some sort) to change it, so its feels really horrible to draw this logic away from domain layer.</p>
  
  <p>Any suggestions are welcome.</p>
  
  <p><strong>Edit 1.</strong>
  After lot of brainstorming i managed to create valid object model for given situation. From first sight it was impossible to encapsulate properties with common constraints, but the latest out-of-domain implementation gave me an idea:</p>
  
  <pre><code>public class Item {
      private Long itemSN;
      private String name;
      private List&lt;Property&gt; properties;
  }
  </code></pre>
  
  <p>Core of the problem was solved here:</p>
  
  <pre><code>public class Property {
      private Long propertyId;
      private String propertyValue;
      private Constraint constraint;
  }
  
  public class Constraint {
      private String name;
      private Boolean required;
      private List&lt;String&gt; allowedValues;
  }
  </code></pre>
  
  <p>So, each property have its value and constraint object which specifies name, allowed values and required status. This way constraint object can be shared by many properites, and any of this properties can have its own value.
  It's adding some complexity to DB mapping and will hit performance but it's also keeping all domain logic in domain objects.</p>
  
  <p>Any improvements, suggestions and opinions are welcome.</p>