需要帮助使Ninject等效于StructureMap语法

时间:2011-04-05 21:24:31

标签: c# ioc-container structuremap ninject ravendb

我正在尝试为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在每个请求都为null。

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

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

4 个答案:

答案 0 :(得分:13)

工厂在Ninject中被称为提供商。将SessionFactory转换为SessionProvider: -

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

答案 1 :(得分:1)

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

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

你事先做过这样的事情:

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

免责声明:我之前从未在Get声明中尝试Bind。您可能需要工厂方法。

答案 2 :(得分:0)

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

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

SingletonScope - 只创建一个实例

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

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

自定义 - 您提供范围对象

如果您正在创建网络应用,则只需指定.InRequestScope()如果是Windows应用,您可以指定.InThreadScope()

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

答案 3 :(得分:0)

您无需创建工厂或提供商等即可执行此操作。

Ninject为每个请求执行会话,为您创建一个Ninject模块,该模块在InSingletonScope()中绑定文档存储,然后在Request范围内绑定DocumentSession并准备就绪。

我为Ninject和RavenDB撰写了一步一步的指南

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

相关问题