title: Value Object with identity
body: <p>In an application we have modelled <code>Company</code> as an Entity and <code>Address</code> as a value object:</p>
  
  <pre><code>public class Company : Entity  {
      public Address PrimaryAddress { get; set; }
      public Address SecondaryAddress { get; set; }
  }
  
  public class Address : ValueObject {
      public string ZipCode { get; private set; } // etc.
  
      public Address(string zipCode) {
          ZipCode = zipCode;
      }
  }
  </code></pre>
  
  <p>Here, <code>Address</code> does not have an identity and is immutable. To update the <code>PrimaryAddress</code> of a company I replace it with a new <code>Address</code> object.</p>
  
  <p>However, after further discovery into the domain we found that a company may have a variable number of addresses. Representing them as individual properties was no longer feasible. So we refactored <code>Company</code>:</p>
  
  <pre><code>public class Company : Entity  {
      public Address[] AddressBook { get; set; }
  }
  </code></pre>
  
  <p>We now have a problem of how to update a company's <code>Address</code>. We <em>now</em> care about an address' identity, but <strong>only within the context of a Company</strong>.</p>
  
  <p>After refactoring we end up with:</p>
  
  <pre><code>public class Company : Entity  {
      public Address[] AddressBook { get; set; }
  
      public void UpdateAddress(Address newAddress) {
          var oldAddress = AddressBook.FirstOrDefault(a =&gt; a.Id == newAddress.Id);
          if (oldAddress != null)
              oldAddress = newAddress;
      }
  }
  
  public class Address : ValueObject {
      public Guid Id { get; private set; }
      public string ZipCode { get; private set; } // etc. 
  
      public Address(Guid id, string zipCode) {
          ZipCode = zipCode;
          Id = id;
      }
  }
  
  // usage
  var company = repo.Load&lt;Company&gt;(companyId);
  var address = new Address(model.Id, "12345"); // uses id of address we are replacing
  
  company.UpdateAddress(address);
  repo.Save(company);
  </code></pre>
  
  <p>To update an address we locate it using its <code>Id</code> and replace it with a new address object with the <strong>same</strong> Id.</p>
  
  <ul>
  <li>Therefore is Address still a value object?</li>
  <li>Am I right to use the <em>same</em> Id for the updated Address, or by definition (as a value object) should we <strong>replace</strong> the Id.</li>
  <li>If the Address only has identity within the context of the Company entity, should we make this explicit in our model, perhaps generating an Id that is only unique inside the entity (perhaps using the entity's id as part of the calculation)?</li>
  </ul>