title: Linq to Entities - 3-tier Architecture
body: <p>In the past few months I've learned alot about Linq-To-Entities and the 3-tier architecture with a DAO/DAL/Repository. Now I've a few things in my mind that keeps bugging me. I've those three questions that you'll see below.</p>
  
  <p>There are alot of ways to make the repository work but what way is "the" way of making the repository work in ways of performance.</p>
  
  <p>1) <strong>Initialize a datacontext in the constructor</strong></p>
  
  <pre><code>public class Repository : IRepository
  {
  
      private Datacontext context;
  
      public Repository()
      {
          context = new Datacontext();
      }
  
      public IList&lt;Entity&gt; GetEntities()
      {
          return (from e in context.Entity
                  select e).ToList();
      }
  }
  </code></pre>
  
  <p>2) <strong>Use "Using"</strong></p>
  
  <pre><code>public class Repository : IRepository
  {
      public IList&lt;Entity&gt; GetEntities()
      {
          using (Datacontext context = new Datacontext())
          {
              return (from e in context.Entity
                      select e).ToList();
          }
  
      }
  }
  </code></pre>
  
  <p>3) <strong>In another way (please comment)</strong></p>
  
  <p><em>I'll put your suggestion here for others to comment</em></p>
  
  <hr>
  
  <p>Also it seems some people say the repository should return an IQueryable to the businesslayer while others say it's better to return an IList. What is your oppinion on this?</p>
  
  <hr>
  
  <p>The above code samples in the first question are pointing to the Repository, but what is the bestway to implement the repository in the businesslayer (Initialize in the constructor, use "Using"??)</p>