Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 EF Unit of Work + Generic Repository + Ninject
    primarykey
    data
    text
    <p>I'm new to MVC3 and have been following the awesome tutorials on the asp.net website. However, I can't quite wrap my head around how to use Unit of Work and Generic Repository patterns with Ninject. I used this tutorial as a starting point: <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application</a></p> <p>Without using interfaces, I know I can implement it like so:</p> <p>Generic Repository:</p> <pre><code>public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class { internal MyContext context; internal DbSet&lt;TEntity&gt; dbSet; public GenericRepository(MyContext context) { this.context = context; this.dbSet = context.Set&lt;TEntity&gt;(); } } </code></pre> <p>Unit of Work:</p> <pre><code>private MyContext context = new MyContext(); private GenericRepository&lt;Student&gt; studentRepository; private GenericRepository&lt;Course&gt; courseRepository; public GenericRepository&lt;Student&gt; StudentRepository { if (this.studentRepository == null) { this.studentRepository = new GenericRepository&lt;Student&gt;(context); } return studentRepository; } public GenericRepository&lt;Course&gt; CourseRepository { if (this.courseRepository == null) { this.courseRepository = new GenericRepository&lt;Course&gt;(context); } return courseRepository; } </code></pre> <p>This setup allows me to pass the same context to all repositories, and then call a single Save() function to commit the changes.</p> <p>I know I can use an interface <code>IGenericRepository&lt;TEntity&gt;</code> and the concrete implementation <code>GenericRepository&lt;TEntity&gt;</code> and then bind them using Ninject:</p> <pre><code>kernel.Bind(typeof(IGenericRepository&lt;&gt;)).To(typeof(GenericRepository&lt;&gt;)); </code></pre> <p>But how would I go about setting up my <code>IUnitOfWork</code> and <code>UnitOfWork</code> to ensure that all my repositories share a single database context? Am I even doing it right in the first place? I've searched around but all I seem to find are tutorials that only use generic repositories without a unit of work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload