title: Null value objects in NHibernate
body: <p>I have a person entity containing an Address as a value object:</p>
  
  <pre><code>public Person()
  {
      WithTable("Person");
      Id(x =&gt; x.Id);
      Component&lt;Address&gt;(x =&gt; x.Address, a =&gt;
      {
          a.Map(x =&gt; x.Address1);
          a.Map(x =&gt; x.Address2);
          a.Map(x =&gt; x.Address3);
          a.Map(x =&gt; x.Town);
          a.Map(x =&gt; x.Postcode);
      });
  }
  </code></pre>
  
  <p>It states in the NHibernate docs that if all the properties of a value object (Address1, Address2 etc) are null, the entire component will be mapped as null (i.e. Person.Address will be null). This is giving me problems in cases where all address fields are null because in my pages where I might have (I'm doing ASP MVC): </p>
  
  <pre><code>&lt;%= Html.TextBoxFor((x =&gt; x.Address.Address1))%&gt;
  </code></pre>
  
  <p>This breaks with a null reference exception. So I'm looking for a clean way to set Address as a new Address() object rather than null if all the fields are empty when I load a Person from the database without doing it manually. I've discounted the following ideas:</p>
  
  <p>Doing null checking in my view (yuk, horrible)</p>
  
  <p>Making database fields not-nullable (I'm dealing with a legacy database)</p>
  
  <p>Anyone any ideas?</p>