Ninject UnitOfWork混乱

时间:2012-05-09 17:03:15

标签: asp.net-mvc-3 unit-of-work ninject.web.mvc

我一直在使用Ninject和我的MVC 3应用程序,但我正在尝试更改我的数据对象的模式以使用UnitOfWork,而我无法弄清楚如何让Ninject正确处理这个问题。

我知道在我的控制台应用程序中手动构建类时,我的类实现是否有效:

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);
}

在我的MVC 3应用程序中,我正在使用Ninject.MVC3 NuGet包,这是在RegisterServices方法中。

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
    kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();

    kernel.Bind<IBlogCategoryDao>().To<BlogCategoryDao>();
    kernel.Bind<IBlogDao>().To<BlogDao>();

    kernel.Bind<IBlogCategoryService>().To<BlogCategoryService>();
    kernel.Bind<IBlogService>().To<BlogService>();
} 

虽然这大部分都适用于Get请求,但所有POST请求(插入,更新,删除)都不会被执行。抛出没有异常,当我单步执行它时,它会顺利通过SaveChanges()方法并返回堆栈,但没有执行任何操作。所以我知道我必须在Ninject配置中遗漏一些东西。

这是我的工作单元课程。

public class UnitOfWork : IUnitOfWork
{
    private Database _database;  <-- 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();
    }
}

这是DatabaseFactory类:

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();
        }
    }
}

我的DataObjectFactory类:

public static class DataObjectFactory
{
    private static readonly string _connectionString;

    /// <summary>
    /// Static constructor. Reads the connectionstring from web.config just once.
    /// </summary>
    static DataObjectFactory()
    {
        string connectionStringName = ConfigurationManager.AppSettings.Get("ConnectionStringName");
        _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    }

    /// <summary>
    /// Creates the Context using the current connectionstring.
    /// </summary>
    /// <remarks>
    /// Gof pattern: Factory method. 
    /// </remarks>
    /// <returns>Action Entities context.</returns>
    public static Database CreateContext()
    {
        return new Database(_connectionString);
    }
}

这与EFMVC CodePlex应用程序中使用的模式类似,但我不使用AutoFac。

对此的任何想法都表示赞赏。

感谢。

1 个答案:

答案 0 :(得分:4)

我这样做:

kernel.Bind<IUnitOfWork>.To<EFUnitOfWork>().InRequestScope();

EFUnitOfWork.cs

public class EFUnitOfWork : DbContext, IUnitOfWork
{
    // your normal DbContext plus your IUnitOfWork members that delegate to EF context
}

由于EF已经实现了一种工作单元形式,因此您可以使用更通用的接口,并轻松注入。

此外,您可以为连接字符串实现EF构造函数,并将它们传递给基础构造函数。然后,您可以使用Ninject .WithConstructorArgument()使用AppSettings代码配置连接字符串。