asp.net-mvc-3 - ASP.NET MVC3 中带有 Ninject 的 RavenDB

标签 asp.net-mvc-3 ravendb ninject.web.mvc

我想在我的 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/

相关文章:

ravendb - 如何让 RavenDB 识别插件?

c# - 将依赖项注入(inject) DelegatingHandler

c# - ASP.NET MVC : No IUserTokenProvider is registered using Ninject

asp.net-mvc - 在 ASP.NET Intranet MVC 应用程序中维护经过身份验证的用户详细信息的最佳实践是什么

c# - 多 map /减少在 RavenDb 中工作吗?

asp.net - 如何拒绝访问直接 URL 到我的部分 View ?

c# - RavenDb 从不同来源导入数据,如 xml、cvs

c# - 使用基于 Ninject 约定的绑定(bind)的正确方法是什么?

MVC3 下的 Facebook OAuth2 和 DotNetOpenAuth

asp.net-mvc-3 - 在服务器上运行 MVC3 应用程序会导致 403.14 响应