Ninject的UnitOfWork混乱 [英] Ninject UnitOfWork confusion

查看:113
本文介绍了Ninject的UnitOfWork混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Ninject所有的时间与我的MVC 3应用程序,但我试图改变模式为我的数据对象使用的UnitOfWork和我有麻烦搞清楚如何让Ninject妥善处理这个问题。

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.

我知道,当他们是手动构造像这样在我的控制台应用程序我的实现类的工作:

I know my implementation of classes work when they are constructed manually like this in my console application:

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

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

In my MVC 3 application I'm using the Ninject.MVC3 NuGet package, and this is in the RegisterServices method.

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

尽管这适用于大多数情况下,获取的要求,所有的POST请求(插入,更新,删除)没有得到执行。有没有例外抛出,当我通过它一步,它通过调用SaveChanges()方法没有问题,并返回堆栈,但执行任何操作。所以我知道我必须失去了我的Ninject配置什么的。

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.

这是我的工作类的单位。

Here's my Unit of Work class.

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类:

Here's the DatabaseFactory class:

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类:

And my DataObjectFactory class:

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。

This is a similar pattern as used in the EFMVC CodePlex application, but I don't use AutoFac.

任何对此的思考是AP preciated。

Any thoughts on this are appreciated.

感谢。

推荐答案

我只是这样做:

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

EFUnitOfWork.cs

EFUnitOfWork.cs

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

由于EF已经实现了单位工作的一种形式,这可以让你用一个更通用的接口,用于它,并轻松地将它。

Since EF already implements a form of Unit Of Work, this allows you to use a more generic interface for it, and inject it easily.

此外,还可以实现对连接字符串的EF构造,只是它们传递到基础构造。然后你可以使用Ninject .WithConstructorArgument()使用您的AppSettings code配置的连接字符串。

Also, you can implement the EF constructors for connection strings and just pass them to base constructors. Then you can use the Ninject .WithConstructorArgument() to configure the connection string using your AppSettings code.

这篇关于Ninject的UnitOfWork混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆