【问题标题】:Manage multiple ravendb document stores through castle windsor in an MVC app?在 MVC 应用程序中通过城堡温莎管理多个 ravendb 文档存储?
【发布时间】:2013-05-30 14:18:49
【问题描述】:

我想出了一个可行的解决方案,将 RavenDB 中的多个数据库用于使用 Castle Windsor 进行布线的 ASP.Net MVC 应用程序。

这是当前的安装程序

public class RavenInstaller : IWindsorInstaller
{

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component.For<IDocumentStore>().Instance(CreateDocumentStore()).LifeStyle.Singleton,
            Component.For<IDocumentSession>().UsingFactoryMethod(GetDocumentSesssion).LifeStyle.PerWebRequest
            );
    }

    static IDocumentStore CreateDocumentStore()
    {
        var store = new DocumentStore { ConnectionStringName = "RavenDb_CS9" };
        store.Initialize();
        IndexCreation.CreateIndexes(typeof(Users).Assembly, store);
        return store;
    }

    static IDocumentSession GetDocumentSesssion(IKernel kernel)
    {
        var store = kernel.Resolve<IDocumentStore>();
        return store.OpenSession();
    }

}

上述方法完美但仅适用于一个数据库

我找不到如何处理另一个数据库的正确想法。整个链从请求IDocumentSession 的域服务开始。然后流程如上述安装程序中指定。但是我在哪里/如何要求“SessionToDb1”或“SessionToDb2”?

当然重要的是要使用什么连接字符串(指定了 DB 属性),还有要在各自的 DB/DocumentStore 中创建什么索引。

有人使用 Windsor 完成此任务吗?我在这里想/攻击错了吗?

谢谢!

【问题讨论】:

  • 此链接可能对您有所帮助:stackoverflow.com/questions/3531552/…
  • 谢谢!我目前正在研究配置选项 .Named() ,您可以在其中命名每个注册并在解析时使用它(而不是类型)。有希望。您的链接让我朝着正确的方向前进。

标签: asp.net-mvc castle-windsor ravendb ioc-container


【解决方案1】:

因为你有:

Component.For<IDocumentSession>()
         .UsingFactoryMethod(GetDocumentSesssion)
         .LifeStyle.PerWebRequest

您的GetDocumentSession 方法将在您注入IDocumentSession 的任何时候被调用。这很好。

使用多个数据库时,您需要将数据库名称作为参数传递给OpenSession。因此,您需要一些方法来根据当前的 Web 请求来解析要连接的数据库。

您需要修改GetDocumentSession 方法来实现您要使用的任何自定义逻辑。例如,您可能想要查看 cookie、asp.net 会话项、当前线程主体或其他一些标准。该决定是您的应用程序自定义的,重要的是您以正确的数据库名称打开会话。

【讨论】:

  • 正是我的问题:) 这对我来说有点奇怪。我目前正在研究 .Named() 组件注册选项。我也认为某种“全局”条件是不合适的,调用代码应该定义它。谢谢!
【解决方案2】:

我之前在使用 nhibernate 时遇到过这个问题。

我发现最好的解决方案是创建一个 SessionManager 类,它包装了文档存储的创建和 Session..

所以我.E.

public interface ISessionManager
{
    void BuildDocumentStore();
    IDocumentSession OpenSession();
}

public interface ISiteSessionManager : ISessionManager
{

}

public class SiteSessionManager : ISiteSessionManager
{
    IDocumentStore _documentStore;

    public SiteSessionManager()
    {
        BuildDocumentStore();
    }

    public void BuildDocumentStore()
    {
        _documentStore = new DocumentStore
        {
            Url = "http://localhost:88",
            DefaultDatabase = "test"
        };

        _documentStore.Initialize();
        IndexCreation.CreateIndexes(typeof(SiteSessionManager).Assembly, _documentStore);
    }

    public IDocumentSession OpenSession()
    {
        return _documentStore.OpenSession();
    }
}

// And then!. 
Container.Register(Component.For<ISiteSessionManager>().Instance(new SiteSessionManager()).LifestyleSingleton());

// And then!.
public class FindUsers
{
    readonly ISiteSessionManager _siteSessionManager;

    public FindUsers(ISiteSessionManager siteSessionManager)
    {
        _siteSessionManager = siteSessionManager;
    }

    public IList<User> GetUsers()
    {
        using (var session = _siteSessionManager.OpenSession())
        {
            // do your query
            return null;
        }
    }
}

冲洗并重复多个数据库。!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多