title: EF - Persistence Ignorance for Detach object - Best Practice
body: <p>Nowdays I cannot understand at all how the persistence ignorance could be applied with entity framework.
  I've a classic repository and unit of work design by using entity framework.</p>
  
  <p>Now, imagine a simple client side scenario where I need to read some object list, and later saving one of them.
  My entity contains some object graph, for instance, the Person entity coontains Address complex property where Addrress is another entity and also a list of Hobby objects, where it also is another entity.
  My client:</p>
  
  <pre><code>var personList = PersonRepository.GetAll();
  PersonRepository.Dispose(); // it will dispose EF's ObjectContext 
  var person = personList[0];
  person.Address.City = "...";
  person.Hobby.Remove(person.Hobby.First());
  PersonRepository.SaveChanges(person);
  </code></pre>
  
  <p>As you can see, I've mantained the persistence ignorance logic, I'm limited to use person object only and its boundaries, however, in real world, tha above code snippet will be inclined to fail for detach object beacuse I've disposed the unit of work.</p>
  
  <p>So, I've thought to use some entity wrapper with some method like 
  ManageGraph(object obj, ObjectState state) in order to preserve any other change in a internal dictionary for all relations changed. However, I don't like this solution, it seems too dirty and looks like anty pattern:</p>
  
  <pre><code>person.Name = "some name";
  person.ManageGraph(AddressObject, state.Modified);
  rather then
  person.ManageGraph(AddressObject, state.Added);
  </code></pre>
  
  <p>But...Where is the persistence ignorance? Why I'm enforce to use bad tricks in order to get the right work? Persistence ignorance means that I must treat the simple object without any other stuff, so it would be like following:</p>
  
  <pre><code>person.SomeProperty = SomeValue
  person.ComplexProperty.OtherProperty = otherValue
  person.ListOfComplexProperty.Add(new entity);
  </code></pre>
  
  <p>For sure, I know, I don't want the magic solution, it doesn't exist, but, is there some good solution or suggestion to get best work?</p>