title: How to create a Required Field Value Object
body: <p>I'm trying to create a <em>Required Field</em> Value Object class that will be reusable across Entities in my Domain Model.  I'm still learning the C# syntax (been coding in VB.net forever).  And I'm all new to DDD (but have at least read several books).</p>
  
  <p>My goal is to design a Value Object called <code>RequiredField&lt;T&gt;</code> that can accept just about any object (e.g. value type, reference type, or nullable value type (int, string, int?)) which can then be validated before allowing it to become part of an Entity's state.  I would use this Value Object any time my entity has a required field (e.g. id, primary key, name, or any other piece of state deemed necessary to have a valid entity).  So the Entity will define properties as <code>RequiredField&lt;T&gt;</code> whenever I have a simple piece of data that is required in order to have a valid Entity.</p>
  
  <p>So I've been fiddling around with this class, and I'm getting closer, but it seems like every time I think I've just about got it, I run in to another stumbling block.  Here is what I would like the usage to look like, from my Entity class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class PersonEntity
  {
      public RequiredField&lt;long&gt; ID { get; private set; }
      public RequiredField&lt;string&gt; Name { get; private set; }
      public RequiredField&lt;DateTime&gt; DOB { get; private set; }
      // define other non-required properties ...
  
      public PersonEntity(PersonDTO dto)
      {
          ID = new RequiredField&lt;long&gt;(dto.ID);
          Name = new RequiredField&lt;string&gt;(dto.Name);
          DOB = new RequiredField&lt;DateTime&gt;(dto.DOB);
          // set other non-required properties ...
  
      }
  }
  </code></pre>
  
  <p>The corresponding DTO that is used to construct the Entity (created in the repository, or application service from the UI, or from a WebService, etc):</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class PersonDTO
  {
      public long? ID { get; set; }
      public string Name { get; set; }
      public DateTime? DOB { get; set; }
  }
  </code></pre>
  
  <p>Note that I really desire the ability to have the DTO be just a data bag (that's basically all a DTO is right?).  If I don't allow nullable types here, then I have to do validation somewhere else, and the whole point is to let the Value Object in the Entity do the work (right?).</p>
  
  <p>Finally, here is what I have so far on my <code>RequiredField&lt;T&gt;</code> class.  Note that this code doesn't compile.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class RequiredField&lt;T&gt;
  {
      private T _value;
      public T Value
      {
          get { return _value; }
          set
          {
              // handle special case of empty string:
              if (typeof(T) == typeof(string) &amp;&amp; string.IsNullOrWhiteSpace((string)value))
              // but (string)value doesn't work: "Can't convert type 'T' to 'string'"
              {
                  throw new ArgumentException("A required string must be supplied.");
              }
              else if (value == null)
              {
                  throw new ArgumentException("A required field must be supplied.");
              }
              // also need to handle Nullable&lt;T&gt;, but can't figure out how
              else if (Nullable.GetUnderlyingType(typeof(T)) != null)
              // need to check value, not T
              {
                  throw new ArgumentException("A required field must be supplied.");
              }
              _value = value;
          }
      }
      public RequiredField(T value)
      {
          Value = value;
      }
      // below is the start of failed attempt to accept a Nullable&lt;T&gt;
      // don't like the fact that I have validation going on here AND in the setter
      public RequiredField(object value)
      {
          if (!value.HasValue)
          {
              throw new ArgumentException("A required field must be supplied.");
          }
          Value = value.Value;
      }
  }
  </code></pre>
  
  <p>So I've gotten myself pretty far into a mess, and I started to question if I'm attempting to do the right thing here.  But if I am on a good start, what gets me over the finish line?</p>