title: Best practices for using the Entity Framework with WPF DataBinding
body: <p>I'm in the process of building my first real WPF application (i.e., the first intended to be used by someone besides me), and I'm still wrapping my head around the best way to do things in WPF.  It's a fairly simple data access application using the still-fairly-new Entity Framework, but I haven't been able to find a lot of guidance online for the best way to use these two technologies (WPF and EF) together.  So I thought I'd toss out how I'm approaching it, and see if anyone has any better suggestions.</p>
  
  <ul>
  <li><p>I'm using the Entity Framework with SQL Server 2008.  The EF strikes me as both much more complicated than it needs to be, and not yet mature, but Linq-to-SQL is apparently dead, so I might as well use the technology that MS seems to be focusing on.</p></li>
  <li><p>This is a simple application, so I haven't (yet) seen fit to build a separate data layer around it.  When I want to get at data, I use fairly simple Linq-to-Entity queries, usually straight from my code-behind, e.g.:</p>
  
  <pre><code>var families = from family in entities.Family.Include("Person")
             orderby family.PrimaryLastName, family.Tag
             select family;
  </code></pre></li>
  <li><p>Linq-to-Entity queries return an IOrderedQueryable result, which doesn't automatically reflect changes in the underlying data, e.g., if I add a new record via code to the entity data model, the existence of this new record is not automatically reflected in the various controls referencing the Linq query.  Consequently, I'm throwing the results of these queries into an ObservableCollection, to capture underlying data changes:</p>
  
  <pre><code>familyOC = new ObservableCollection&lt;Family&gt;(families.ToList());
  </code></pre></li>
  <li><p>I then map the ObservableCollection to a CollectionViewSource, so that I can get filtering, sorting, etc., without having to return to the database.</p>
  
  <pre><code>familyCVS.Source = familyOC;
  familyCVS.View.Filter = new Predicate&lt;object&gt;(ApplyFamilyFilter);
  familyCVS.View.SortDescriptions.Add(new System.ComponentModel.SortDescription("PrimaryLastName", System.ComponentModel.ListSortDirection.Ascending));
  familyCVS.View.SortDescriptions.Add(new System.ComponentModel.SortDescription("Tag", System.ComponentModel.ListSortDirection.Ascending));
  </code></pre></li>
  <li><p>I then bind the various controls and what-not to that CollectionViewSource:</p>
  
  <pre><code>&lt;ListBox DockPanel.Dock="Bottom" Margin="5,5,5,5" 
      Name="familyList" 
      ItemsSource="{Binding Source={StaticResource familyCVS}, Path=., Mode=TwoWay}" 
      IsSynchronizedWithCurrentItem="True" 
      ItemTemplate="{StaticResource familyTemplate}" 
      SelectionChanged="familyList_SelectionChanged" /&gt;
  </code></pre></li>
  <li><p>When I need to add or delete records/objects, I manually do so from both the entity data model, and the ObservableCollection:</p>
  
  <pre><code>private void DeletePerson(Person person)
  {
      entities.DeleteObject(person);
      entities.SaveChanges();
      personOC.Remove(person);
  }
  </code></pre></li>
  <li><p>I'm generally using StackPanel and DockPanel controls to position elements.  Sometimes I'll use a Grid, but it seems hard to maintain: if you want to add a new row to the top of your grid, you have to touch every control directly hosted by the grid to tell it to use a new line.  Uggh.  (Microsoft has never really seemed to get the DRY concept.)  </p></li>
  <li><p>I almost never use the VS WPF designer to add, modify or position controls.  The WPF designer that comes with VS is sort of vaguely helpful to see what your form is going to look like, but even then, well, not really, especially if you're using data templates that aren't binding to data that's available at design time.  If I need to edit my XAML, I take it like a man and do it manually.</p></li>
  <li><p>Most of my real code is in C# rather than XAML.  As I've mentioned <a href="http://blog.wouldbetheologian.com/2009/03/further-thoughts-on-wpf-and-xaml.html" rel="noreferrer">elsewhere</a>, entirely aside from the fact that I'm not yet used to "thinking" in it, XAML strikes me as a clunky, ugly language, that also happens to come with poor designer and intellisense support, and that can't be debugged.  Uggh.  Consequently, whenever I can see clearly how to do something in C# code-behind that I can't easily see how to do in XAML, I do it in C#, with no apologies.  There's been plenty written about how it's a good practice to  almost never use code-behind in WPF page (say, for event-handling), but so far at least, that makes no sense to me whatsoever.  Why should I do something in an ugly, clunky language with god-awful syntax, an astonishingly bad editor, and virtually no type safety, when I can use a nice, clean language like C# that has a world-class editor, near-perfect intellisense, and unparalleled type safety?</p></li>
  </ul>
  
  <p>So that's where I'm at.  Any suggestions?  Am I missing any big parts of this?  Anything that I should really think about doing differently?</p>