title: best practice to implement repository pattern and unitOfWork in ASP.NET MVC
body: <p>I am working on implementing Repository Pattern and UnitOfWork from last few days, which I have completed to upto good extend, I believe. I am sure there are plenty of ways to implement that but what I am interesting to find best approach for that. </p>
  
  <p>I am taking very simple example coded in ASP.NET MVC 5 using visual studio 2013. my main focus of question is implementation of UnitOfWork. is it advisable to use multiple UnitOfWorks for each business concerns implementing repository functions in private method and giving away public functions for controller to use????</p>
  
  <p>I have function table (SQL Server) in the controller class via generic repository. I have IGenericRepository which has IQueryable one function, I have GenericRepository class where i am implementing this interface. I got FunctionContext which is inherited from baseContext. The reason i have baseContext so all the dbcontexts can use one path to hit database but same time keep number of table limited to business need.</p>
  
  <p>now in my approach;</p>
  
  <ol>
  <li>One BaseContext : DbContext</li>
  <li>multiple DbContext, bundling all required table to individual business concern that extend BaseContext</li>
  <li>Generic Repository Interface (CRUD)</li>
  <li>Generic Repository Implementation class</li>
  <li>specific Repository class, extending Generic Reposirtory in case require more function on top of CRUD operations.</li>
  <li>Individual UnitOfWork --> taking to required repository/ repositories in private method and provide public method only for using functions</li>
  <li>Controller call require UnitOfWork to use public methods.</li>
  </ol>
  
  <p>in following program, all i am getting list of function title and passing to controller to print</p>
  
  <h2>Generic Repository Interface</h2>
  
  <pre><code> public interface IGenericRepository&lt;TEntity&gt; : IDisposable
  {
      IQueryable&lt;TEntity&gt; GetAll(); 
  
  }
  </code></pre>
  
  <h2>Generic Repository</h2>
  
  <pre><code>public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class
  {
      protected DbSet&lt;TEntity&gt; _DbSet;
      private readonly DbContext _dbContext;
  
      public GenericRepository()
      {
  
      }
  
      public GenericRepository(DbContext dbContext)
      {
          this._dbContext = dbContext;
          _DbSet = _dbContext.Set&lt;TEntity&gt;();
      }
  
  
  
      public IQueryable&lt;TEntity&gt; GetAll()
      {
          return _DbSet;
      }
  
  
      public void Dispose()
      {
  
      }
  }
  </code></pre>
  
  <h2>BaseContext</h2>
  
  <pre><code> public class BaseContext&lt;TContext&gt; : DbContext where TContext : DbContext
  {
      static BaseContext()
      {
          Database.SetInitializer&lt;TContext&gt;(null);
      }
  
      protected BaseContext()
          : base("name = ApplicationDbConnection")
      { }
  }
  </code></pre>
  
  <h2>FunctionContext</h2>
  
  <pre><code> public class FunctionsContext : BaseContext&lt;FunctionsContext&gt;
  {
      public DbSet&lt;App_Functions&gt; Functions { get; set; }
  }
  </code></pre>
  
  <h2>Function Mapping class</h2>
  
  <pre><code>[Table("Functions")]
  public class App_Functions
  {
       public App_Functions()
      {
      }
  
      [Key]
      public int Function_ID { get; set; }
  
      [StringLength(50)]
      [Required]
      public string Title { get; set; }
  
      public int Hierarchy_level { get; set; }
  }
  </code></pre>
  
  <h2>Function Domain class</h2>
  
  <pre><code> public class Functions
  {
      public Functions()
      {
  
      }
  
      public int Function_ID { get; set; }
      public string Title { get; set; }
      public int Hierarchy_level { get; set; }
  }
  </code></pre>
  
  <h2>Function_UnitOfWork</h2>
  
  <pre><code>public class Function_UnitOfWork
  {
     private GenericRepository&lt;App_Functions&gt; _function_Repository = new GenericRepository&lt;App_Functions&gt;(new FunctionsContext());
  
     public Function_UnitOfWork()
     {
  
     }
  
     private List&lt;Functions&gt; getAllFunctionsFromRepository()
     {
         List&lt;Functions&gt; query = new List&lt;Functions&gt;();
  
         query = _function_Repository.GetAll().Select(x =&gt; new Functions
         {
             Function_ID = x.Function_ID,
             Title = x.Title,
             Hierarchy_level = x.Hierarchy_level
         }).ToList();
  
         return query; 
     }
  
     public List&lt;Functions&gt;GetAllFunctions()
     {
         return getAllFunctionsFromRepository();
     }
  
  
  
  }
  </code></pre>
  
  <h2>Controller class</h2>
  
  <pre><code>public class HomeController : Controller
  {
      public ActionResult Index()
      {
           Function_UnitOfWork UOF = new Function_UnitOfWork();
  
         var a1 = UOF.GetAllFunctions();
  
          foreach(var item in a1)
          {
              var b1 = item.Title;
          }
  
  
          return View();
      }
  }
  </code></pre>