[英]Need help getting Ninject equivalent for StructureMap syntax
I am trying to implement IoC (Ninject) for Ravendb and have ran into a little snag. 我正在尝试为Ravendb实现IoC(Ninject)并遇到了一些障碍。 I am using code from http://www.dotnetguy.co.uk/post/2010/06/12/raven-db-ndash-part-1-ndash-documentsession-per-request-with-structuremap to help. 我正在使用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();
}
}
I am unsure how to convert the following structure map syntax. 我不确定如何转换以下结构映射语法。
ObjectFactory.Configure(x => x.For<IDocumentSession>()
.HybridHttpOrThreadLocalScoped()
.AddInstances(inst => inst.ConstructedBy
(context => context.GetInstance<IRavenSessionFactoryBuilder>()
.GetSessionFactory().CreateSession())));
In My attempt, _ravenSessionFactory is null on every request because of the new constructor. 在我的尝试中,由于新的构造函数,_ravenSessionFactory在每个请求上都为null。
Bind<IDocumentSession>().ToMethod(
x => new RavenSessionFactoryBuilder().GetSessionFactory().CreateSession()).RequestScope();
Thanks for anyone taking the time to try and help explain. 感谢任何人花时间尝试帮助解释。
Factories are called providers in Ninject. 工厂在Ninject中被称为提供者。 Convert the SessionFactory
to a SessionProvider
:- 将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();
}
}
Also change your RavenSessionFactoryBuilder to a DocumentStoreProvider:- 还要将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;
}
}
And add bindings: 并添加绑定:
Bind<RavenSessionProvider>().ToSelf().InSingletonScope()
Bind<IDocumentSession>().ToProvider<RavenSessionProvider>();
Instead of new RavenSessionFactoryBuilder().GetSessionFactory()....
, I would think you'd want: 而不是new RavenSessionFactoryBuilder().GetSessionFactory()....
,我认为你想要:
Kernel.Get<IRavenSessionFactoryBuilder>().GetSessionFactory()....
where you've done something like this beforehand: 事先你做过这样的事情:
Bind<IRavenSessionFactoryBuilder>().To<IRavenSessionFactoryBuilder>()
.InSingletonScope();
Disclaimer: I've never tried a Get
in a Bind
statement before. 免责声明:我之前从未尝试过Get
in a Bind
声明。 You might need a factory method. 您可能需要工厂方法。
You don't need to create a factory or provider etc to do this. 您不需要创建工厂或提供程序等来执行此操作。
Ninject does the session per request work for you create a Ninject module that binds a document store in InSingletonScope(), then bind a DocumentSession in Request scope and your ready to go. Ninject为每个请求执行会话,为您创建一个Ninject模块,该模块在InSingletonScope()中绑定文档存储,然后在Request范围内绑定DocumentSession并准备就绪。
I've blogged a step by step guide for Ninject and RavenDB 我已经为Ninject和RavenDB发布了一步一步的指南
http://www.dalsoft.co.uk/blog/index.php/2012/04/12/mvc-get-ravendb-up-and-running-in-5-minutes-using-ninject/ http://www.dalsoft.co.uk/blog/index.php/2012/04/12/mvc-get-ravendb-up-and-running-in-5-minutes-using-ninject/
Ninject basically has 5 options for scope. Ninject基本上有5个范围选项。
TransientScope - the one you're using means that a new instance is created for every request TransientScope - 您正在使用的那个意味着为每个请求创建一个新实例
SingletonScope - only one instance is ever created SingletonScope - 只创建一个实例
ThreadScope - only one instance is created per thread ThreadScope - 每个线程只创建一个实例
RequestScope - only one instance is created per HttpRequest RequestScope - 每个HttpRequest只创建一个实例
Custom - you provide the scope object 自定义 - 您提供范围对象
If you're creating a web app you can just specify .InRequestScope()
If it's a windows app you might specify .InThreadScope()
如果您正在创建一个Web应用程序,您可以指定.InRequestScope()
如果它是一个Windows应用程序,您可以指定.InThreadScope()
Finally if you must specify a hybrid (I'm not totally sure how it works in structure map) you might do .InScope(ctx => HttpRequest.Current != null ? HttpRequest.Current : Thread.CurrentThread)
最后,如果你必须指定一个混合(我不完全确定它在结构图中是如何工作的)你可以做.InScope(ctx => HttpRequest.Current != null ? HttpRequest.Current : Thread.CurrentThread)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.