首页 文章

Ninject请求范围和回调

提问于
浏览
1

我在使用Owin的MVC项目中遇到了Ninject的问题 .

我有一个UnitOfWork的泛型类,它不是我的项目特有的:

public class UnitOfWork : IUnitOfWork
{
    public UnitOfWork(DbContext context)
    {...}
}

我使用自定义DbContext定义了两个存储库:

public UserRepository : IUserRepository
{
    public UserRepository(MyEntities context)
    {...}
}
public OrderRepository : IOrderRepository
{
    public OrderRepository(MyEntities context)
    {...}
}

然后我有一个ApiController,它使用工作单元和存储库 .

public OrderController : ApiController
{
    public OrderController(IUnitOfWork uow, IUserRepository userRepository, IOrderRepository orderRepository)
    {...}
}

我在一个模块中配置我的Ninject内核 . 我的绑定带有请求范围 .

public class MyModule : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        // Bind all the repositories
        this.Bind(x =>
            x.FromAssembliesMatching("*.Repositories")
                .SelectAllClasses()
                .BindDefaultInterface()
                .Configure(c => c.InRequestScope()));

        // Bind the DbContext of the application
        this.Bind<MyEntities>()
            .ToSelf()
            .InRequestScope();

        // To bind the UnitOfWork, I need to specify the real DbContext to use. For that I use a callback which provide argument to constructor :
        this.Bind<IUnitOfWork>()
            .To<UnitOfWork>()
            .InRequestScope()
            .WithConstructorArgument("context", GetContext);
    }

    private Object GetContext(IContext context, ITarget target)
    {
        IResolutionRoot resolver;
        ActivationBlock scope;

        scope = context.Request.GetScope() as ActivationBlock;
        resolver = scope ?? (IResolutionRoot)context.Kernel;

        var o = resolver.Get<MyEntities>();
        var o2 = resolver.Get<MyEntities>();
        var same = Object.ReferenceEquals(o, o2);
        return o;
    }
}

然后我在Startup类中使用Owin激活Ninject:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...
        app.UseNinjectMiddleware(Startup.CreateKernel);

        var config = new HttpConfiguration();
        ...
        app.UseNinjectWebApi(config);
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(new MyModule());
        return kernel;
    }
}

看起来不错但有一个大问题 . 存储库共享相同的DbContext,但UnitOfWork中的DbContext是不同的实例 . 在函数GetContext中,作用域始终为null,因此从内核检索MyContext实例 . 布尔变量same始终为false . 问题出在这里 . 内核的Get函数返回一个新实例,而不是请求范围的实例 .

1 回答

  • 0

    不确定你是否还需要这个...但你可以将dbcontext绑定到self,然后在你想要使用它时请求它 .

    Bind<ApplicationDbContext>().ToSelf();
            Bind<IUserStoreGuid<User>>().To<UserStoreGuid<User>>().WithConstructorArgument("context", Kernel.GetService(typeof(ApplicationDbContext)));
    

    虽然应用程序中的连接字符串名为"DefautConnection",但您需要使用"context",因为这是在构造函数参数中调用它的方式 . 我从here得到了这个

相关问题