我想在我的 asp.net mvc3 项目中使用带有 ninject 的 RavenDB,知道我必须如何配置它吗?
kernel.Bind<Raven.Client.IDocumentSession>()
.To<Raven.Client.Document.DocumentStore>()
.InSingletonScope()
.WithConstructorArgument("ConnectionString", ConfigurationManager.ConnectionStrings["RavenDB"].ConnectionString);
最佳答案
这是我的做法:
如果您使用 Nuget 安装 Ninject,您将获得一个/App_start/NinjectMVC3.cs 文件。在那里:
private static void RegisterServices(IKernel kernel)
{
kernel.Load<RavenModule>();
}
这是 RavenModule 类:
public class RavenModule : NinjectModule
{
public override void Load()
{
Bind<IDocumentStore>()
.ToMethod(InitDocStore)
.InSingletonScope();
Bind<IDocumentSession>()
.ToMethod(c => c.Kernel.Get<IDocumentStore>().OpenSession())
.InRequestScope();
}
private IDocumentStore InitDocStore(IContext context)
{
DocumentStore ds = new DocumentStore { ConnectionStringName = "Raven" };
RavenProfiler.InitializeFor(ds);
// also good to setup the glimpse plugin here
ds.Initialize();
RavenIndexes.CreateIndexes(ds);
return ds;
}
}
为了完整起见,这是我的索引创建类:
public static class RavenIndexes
{
public static void CreateIndexes(IDocumentStore docStore)
{
IndexCreation.CreateIndexes(typeof(RavenIndexes).Assembly, docStore);
}
public class SearchIndex : AbstractMultiMapIndexCreationTask<SearchIndex.Result>
{
// implementation omitted
}
}
我希望这有帮助!
关于asp.net-mvc-3 - ASP.NET MVC3 中带有 Ninject 的 RavenDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521234/