4

我正在尝试为 Ravendb 实现 IoC (Ninject) 并遇到了一些障碍。我正在使用来自http://www.dotnetguy.co.uk/post/2010/06/12/raven-db-ndash-part-1-ndash-documentsession-per-request-with-structuremap的代码来提供帮助。

public interface IRavenSessionFactoryBuilder
{
    IRavenSessionFactory GetSessionFactory();
}

public class RavenSessionFactoryBuilder : IRavenSessionFactoryBuilder
{
    private IRavenSessionFactory _ravenSessionFactory;

    public IRavenSessionFactory GetSessionFactory()
    {
        return _ravenSessionFactory ?? (_ravenSessionFactory = CreateSessionFactory());
    }

    private static IRavenSessionFactory CreateSessionFactory()
    {
        Debug.Write("IRavenSessionFactory Created");
        return new RavenSessionFactory(new DocumentStore
                                           {
                                               Url =
                                                   System.Web.Configuration.WebConfigurationManager.AppSettings[
                                                       "Raven.DocumentStore"]
                                           });
    }
}

public interface IRavenSessionFactory
{
    IDocumentSession CreateSession();
}

public class RavenSessionFactory : IRavenSessionFactory
{
    private readonly IDocumentStore _documentStore;

    public RavenSessionFactory(IDocumentStore documentStore)
    {
        if (_documentStore != null) return;
        _documentStore = documentStore;
        _documentStore.Initialize();
    }

    public IDocumentSession CreateSession()
    {
        Debug.Write("IDocumentSession Created");
        return _documentStore.OpenSession();
    }
}

我不确定如何转换以下结构映射语法。

ObjectFactory.Configure(x => x.For<IDocumentSession>()
                  .HybridHttpOrThreadLocalScoped()
                  .AddInstances(inst => inst.ConstructedBy
                    (context => context.GetInstance<IRavenSessionFactoryBuilder>()
                      .GetSessionFactory().CreateSession())));

在我的尝试中,由于新的构造函数,_ravenSessionFactory 在每个请求上都为空。

Bind<IDocumentSession>().ToMethod(
            x => new RavenSessionFactoryBuilder().GetSessionFactory().CreateSession()).RequestScope();

感谢任何花时间尝试帮助解释的人。

4

4 回答 4

13

在 Ninject 中,工厂被称为提供者。将 转换SessionFactorySessionProvider:-

public class RavenSessionProvider : Provider<IDocumentSession>
{
    private readonly IDocumentStore _documentStore;

    public RavenSessionFactory(IDocumentStore documentStore)
    {
        _documentStore = documentStore;
    }

    public IDocumentSession GetInstance(IContext ctx)
    {
        Debug.Write("IDocumentSession Created");
        return _documentStore.OpenSession();
    }
}

还将您的 RavenSessionFactoryBuilder 更改为 DocumentStoreProvider:-

public class DocumentStoreProvider : Provider<IDocumentStore>
{
    public IDocumentStore GetInstance(IContext ctx)
    {
        var store = new DocumentStore 
                   { Url = System.Web.Configuration.WebConfigurationManager.AppSettings["Raven.DocumentStore"]});
        store.Initialize();
        return store;
    }
}

并添加绑定:

Bind<RavenSessionProvider>().ToSelf().InSingletonScope()
Bind<IDocumentSession>().ToProvider<RavenSessionProvider>();
于 2011-04-06T21:31:34.967 回答
1

而不是new RavenSessionFactoryBuilder().GetSessionFactory()....,我想你会想要:

Kernel.Get<IRavenSessionFactoryBuilder>().GetSessionFactory()....

你事先做过这样的事情:

Bind<IRavenSessionFactoryBuilder>().To<IRavenSessionFactoryBuilder>()
  .InSingletonScope();

免责声明:我以前从未GetBind声明中尝试过。您可能需要一个工厂方法。

于 2011-04-05T22:03:36.467 回答
0

Ninject 基本上有 5 个范围选项。

TransientScope - 您正在使用的意思是为每个请求创建一个新实例

SingletonScope - 只创建一个实例

ThreadScope - 每个线程只创建一个实例

RequestScope - 每个 HttpRequest 只创建一个实例

自定义 - 您提供范围对象

如果您正在创建一个 Web 应用程序,您可以指定.InRequestScope()如果它是一个 Windows 应用程序,您可以指定.InThreadScope()

最后,如果您必须指定一个混合(我不完全确定它在结构图中如何工作),您可能会这样做.InScope(ctx => HttpRequest.Current != null ? HttpRequest.Current : Thread.CurrentThread)

于 2011-04-05T21:35:08.127 回答
0

您不需要创建工厂或提供者等来执行此操作。

Ninject 为您创建一个 Ninject 模块,该模块在 InSingletonScope() 中绑定一个文档存储,然后在请求范围内绑定一个 DocumentSession,然后您就可以开始工作了。

我已经为 Ninject 和 RavenDB 写了一个分步指南

http://www.dalsoft.co.uk/blog/index.php/2012/04/12/mvc-get-ravenb-up-and-running-in-5-minutes-using-ninject/

于 2012-04-16T11:08:48.820 回答