Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject UnitOfWork confusion
    primarykey
    data
    text
    <p>I use Ninject all the time with my MVC 3 applications, but I'm trying to change the Pattern for my Data Objects to use UnitOfWork and I'm having trouble figuring out how to get Ninject to handle this properly.</p> <p>I know my implementation of classes work when they are constructed manually like this in my console application:</p> <pre><code>IDatabaseFactory factory = new DatabaseFactory(); IUnitOfWork worker = new UnitOfWork(factory); IBlogCategoryDao dao = new BlogCategoryDao(factory); IBlogCategoryService service = new BlogCategoryService(dao); BlogCategory category = service.GetById(id); try { if (category != null) { service.Delete(category); worker.Commit(); Console.WriteLine("Category deleted successfully!"); } else { Console.WriteLine("Entity doesn't exist."); } } catch (Exception ex) { Console.WriteLine("Error deleting category: {0}", ex.Message); } </code></pre> <p>In my MVC 3 application I'm using the Ninject.MVC3 NuGet package, and this is in the RegisterServices method.</p> <pre><code>private static void RegisterServices(IKernel kernel) { kernel.Bind&lt;IDatabaseFactory&gt;().To&lt;DatabaseFactory&gt;(); kernel.Bind&lt;IUnitOfWork&gt;().To&lt;UnitOfWork&gt;().InRequestScope(); kernel.Bind&lt;IBlogCategoryDao&gt;().To&lt;BlogCategoryDao&gt;(); kernel.Bind&lt;IBlogDao&gt;().To&lt;BlogDao&gt;(); kernel.Bind&lt;IBlogCategoryService&gt;().To&lt;BlogCategoryService&gt;(); kernel.Bind&lt;IBlogService&gt;().To&lt;BlogService&gt;(); } </code></pre> <p>While this works for the most part, Get requests, all POST requests (Insert, Update, Delete) don't get executed. There is no exception thrown and when I step through it, it goes through the SaveChanges() method without a problem and returns back up the stack, but nothing is executed. So I know I must be missing something with my Ninject configuration.</p> <p>Here's my Unit of Work class.</p> <pre><code>public class UnitOfWork : IUnitOfWork { private Database _database; &lt;-- DbContext derived class private readonly IDatabaseFactory _databaseFactory; public UnitOfWork(IDatabaseFactory databaseFactory) { this._databaseFactory = databaseFactory; } public Database Database { get { return _database ?? (_database = _databaseFactory.Get()); } } public void Commit() { Database.Commit(); } } </code></pre> <p>Here's the DatabaseFactory class:</p> <pre><code>public class DatabaseFactory : Disposable, IDatabaseFactory { private Database _database; public DatabaseFactory() { } public virtual Database Get() { if (_database == null) { _database = DataObjectFactory.CreateContext(); } return _database; } protected override void DisposeCore() { if (_database != null) { _database.Dispose(); } } } </code></pre> <p>And my DataObjectFactory class:</p> <pre><code>public static class DataObjectFactory { private static readonly string _connectionString; /// &lt;summary&gt; /// Static constructor. Reads the connectionstring from web.config just once. /// &lt;/summary&gt; static DataObjectFactory() { string connectionStringName = ConfigurationManager.AppSettings.Get("ConnectionStringName"); _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; } /// &lt;summary&gt; /// Creates the Context using the current connectionstring. /// &lt;/summary&gt; /// &lt;remarks&gt; /// Gof pattern: Factory method. /// &lt;/remarks&gt; /// &lt;returns&gt;Action Entities context.&lt;/returns&gt; public static Database CreateContext() { return new Database(_connectionString); } } </code></pre> <p>This is a similar pattern as used in the EFMVC CodePlex application, but I don't use AutoFac.</p> <p>Any thoughts on this are appreciated.</p> <p>Thanks.</p>
    singulars
    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