title: Repository pattern with Linq to SQL using IoC, Dependency Injection, Unit of Work
body: <p>There seems to be lots of examples on implementing Repository pattern for Linq to SQL. Most of them featuring IRepository and DI; Some have implemented Unit of Work and some not. I tried to read as most of the results returned by searches on SO and Google on Linq to SQL repository patterns. Nevertheless I've not come across a complete solution yet.</p>
  <p>From my readings I've implemented a repository pattern as shown below:</p>
  <p><a href="https://i.stack.imgur.com/yBtgz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yBtgz.png" alt="repository pattern" /></a><br />
  <sub>(source: <a href="https://sites.google.com/site/baburajpb/repositorypattern.png" rel="nofollow noreferrer">baburajpb at sites.google.com</a>)</sub></p>
  <p>I'm using DI to register interfaces on which the repositories are depended:</p>
  <pre><code>this.container.RegisterType&lt;IConnectionStringFactory, ConnectionStringFactory&gt;(new ContainerControlledLifetimeManager(),
      new InjectionConstructor(connectionString));
  this.container.RegisterType&lt;IDataContextFactory, DataContextFactory&gt;();
  </code></pre>
  <p>Implementation of Repository pattern:</p>
  <pre><code>public interface IPrivilegeRepository : IRepository&lt;PrivilegesEntity&gt;
  {
     IList&lt;MenuModel&gt; GetRootMenu();
     IList&lt;MenuModel&gt; GetChildMenu(int parentId);
  }
  
  public class PrivilegeRepository : Repository&lt;PrivilegesEntity&gt;, IPrivilegeRepository
  {
      #region IPrivilegeRepository Members
  
      public IList&lt;MenuModel&gt; GetRootMenu()
      {
          return FindAll(menu =&gt; menu.ParentId == null)
              .OrderBy(menu =&gt; menu.SortOrder)
              .Select(c =&gt; EntityMapper.Privileges.ToBusinessObject(c))
              .ToList();
      }
  
      public IList&lt;MenuModel&gt; GetChildMenu(int parentId)
      {
          return FindAll(menu =&gt; menu.ParentId == parentId)
              .OrderBy(menu =&gt; menu.SortOrder)
              .Select(menu =&gt; EntityMapper.Privileges.ToBusinessObject(menu))
              .ToList();
      }
  
      #endregion
  
      public PrivilegeRepository(IDataContextFactory dataContextFactory)
          : base(dataContextFactory)
      {
      }
  }
  </code></pre>
  <p>IRepository generic interface:</p>
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      IEnumerable&lt;T&gt; All();
      IEnumerable&lt;T&gt; FindAll(Expression&lt;Func&lt;T, bool&gt;&gt; exp);
      T Single(Expression&lt;Func&lt;T, bool&gt;&gt; exp);
      T First(Expression&lt;Func&lt;T, bool&gt;&gt; exp);
  }
  </code></pre>
  <p>Repository class is implemented as below with implementations of IRepository (not shown) and is having a dependency on IDataContextFactory which DI is taking care of:</p>
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      public Repository(IDataContextFactory dataContextFactory)
      {
          this.dataContextFactory = dataContextFactory;
      }
  }
  </code></pre>
  <p>Repositories are consumed using IoC:</p>
  <pre><code>PrivilegeRepository repository = container.Resolve&lt;PrivilegeRepository&gt;();
  </code></pre>
  <p>I'm returning result of queries as a collection of Business Object in order to avoid dependency on Linq to SQL on application layers where I consume repository. The above scenario works fine with my WPF application which is using MVVM pattern. I have ViewModel aks Presenter classes which are not depended of classes generated by Linq-SQL.</p>
  <p><strong>How do I extend this pattern so that I can save data to the Database. I'd like to pass Business Objects back to the repository and get them saved. Is it possible? How can I implement Unit of Work in such a scenario.</strong></p>