title: How to decouple EF objects from UI
body: <p>I have an N-Tier app, UI, BLL and DAL. My DAL uses EF6 for all database interactions.</p>
  
  <p>This means, if I query the database and then want to iterate over the object in my UI (such as a foreach loop to display values on screen) my UI has to know about the DAL. This seems incorrect. I'd like my DAL to know about nothing. The Bll can know of the DAL. And the UI can only know the BLL.</p>
  
  <p>Please consider this simplified EF object</p>
  
  <pre><code>partial class Foo 
  {
      public int Id { get; set; }
      public virtual ICollection&lt;Bar&gt; FooFoo { get; set; }
  }
  </code></pre>
  
  <p>The only way I can think of is if I also have a Bll.Foo which maps the 2 objects. Then I thought, may be it's better if they both implement an Interface of IFoo. But Foo has a Collection of Bar, which also will need to be of type IBar. And this is where it goes wrong. Please consider:</p>
  
  <pre><code>class Foo : IFoo                                          //generated by EF
  {
      public int Id { get; set; }                           //generated by EF
      public virtual ICollection&lt;Bar&gt; FooFoo { get; set; }  //generated by EF and KABOOM
  }
  
  interface IFoo
  {
      ICollection&lt;IBar&gt; FooFoo { get; set; }   
  }
  
  class Bar : IBar
  {
      public int MyPr
      { get; set; }
  }
  
  interface IBar
  {
      int MyPr { get; set; }
  }
  </code></pre>
  
  <p>The issue with the above, where I've shown the Kaboom, is the interface is not 'satisfied' as it comes via EF as type <code>Bar</code>, but the interface says I need to implement it as type <code>IBar</code>. Without this, I'm not decoupling...</p>
  
  <p>How can I achieve this or is my understanding flawed?</p>