title: Question about Repositories and their Save methods for domain objects
body: <p>I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this example, I have 3 classes: <strong>Address</strong>, <strong>Company</strong> and <strong>Person</strong>. A Person is a member of a Company and has an Address. A Company also has an Address. </p>
  
  <p>These classes reflect a database model. I removed any dependencies of my Models, so they are not tied to a particular ORM library such as NHibernate or LinqToSql. These dependencies are dealt with inside the Repositories.</p>
  
  <p>Inside one of <strong>Repositories</strong> there is a <strong>SavePerson(Person person)</strong> method which <strong>inserts/updates</strong> a Person depending on whether it already exists in the database.</p>
  
  <p>Since a Person object has a Company, I currently save/update the values of the Company property too when making that SavePerson call. I insert / update all of the Company's data - Name and Address - during this procedure. </p>
  
  <p>However, I really have a hard time thinking of a case where a Company's data can change while dealing with a Person - I only want to be able to assign a Company to a Person, or to move a Person to another Company. I don't think I ever want to create a new Company alongside a new Person. So the SaveCompany calls introduce unnecessary database calls. When saving a Person I should just be able to update the CompanyId column.</p>
  
  <p>But since the Person class has a Company property, I'm somewhat inclined to update / insert it with it. From a strict/pure point of view, the SavePerson method should save the entire Person.</p>
  
  <p>What would the preferred way be? Just inserting/updating the CompanyId of the Company property when saving a Person or saving all of its data? Or would you create two distinct methods for both scenarios (What would you name them?)</p>
  
  <p>Also, another question, I currently have distinct methods for saving a Person, an Address and a Company, so when I save a Company, I also call SaveAddress. Let's assume I use LinqToSql - this means that I don't insert/update the Company and the Address in the same Linq query. I guess there are 2 Select Calls (checking whether a company exists, checking whether an address exists). And then two Insert/Update calls for both. Even more if more compound model classes are introduced. Is there a way for LinqToSql to optimize these calls?</p>
  
  <pre><code>public class Address
  {
      public int AddressId { get; set; }
      public string AddressLine1 { get; set; }
      public string AddressLine2 { get; set; }
      public string City { get; set; }
      public string PostalCode { get; set; }        
  }
  
  public class Company
  {
      public int CompanyId { get; set; }
      public string Name { get; set; }
      public Address Address { get; set; }
  }
  
  
  public class Person
  {
      public int PersonId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Email { get; set; }
      public Company Company { get; set; }
      public Address Address { get; set; }
  
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>Also see <a href="https://stackoverflow.com/questions/679005/how-are-value-objects-stored-in-the-database">this follow up question</a>. How are Value Objects stored in a Database?</p>