需要帮助获取StructureMap语法的Ninject等价



我正试图为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();

感谢大家花时间帮忙解释

工厂在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>();

代替new RavenSessionFactoryBuilder().GetSessionFactory()....,我想你会想要:

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

你事先做了类似这样的事情:

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

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

Ninject基本上有5个作用域选项。

TransientScope——你正在使用的这个意味着为每个请求创建一个新的实例

SingletonScope -只创建一个实例

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

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

自定义-提供作用域对象

如果你是在创建一个web应用程序你可以指定.InRequestScope()如果是一个windows应用程序你可以指定.InThreadScope()

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

您不需要创建工厂或提供商等来完成此操作。

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/

最新更新