【问题标题】:How to setup multitenancy within an mvc 3 application with ravendb如何使用 ravendb 在 mvc 3 应用程序中设置多租户
【发布时间】:2012-03-02 01:57:02
【问题描述】:

我们通过以下方式使用 RavenDb 设置 mvc3 应用程序(在 NoSql with RavenDb and Asp.net MVC 的帮助下):

以下代码在 Global.asax 中

private const string RavenSessionKey = "RavenMVC.Session";
private static DocumentStore documentStore;

protected void Application_Start()
{
    //Create a DocumentStore in Application_Start
    //DocumentStore should be created once per 
    //application and stored as a singleton.
    documentStore = new DocumentStore { Url = "http://localhost:8080/" };
    documentStore.Initialise();
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    //DI using Unity 2.0
    ConfigureUnity();
}

public MvcApplication()
{
    //Create a DocumentSession on BeginRequest  
    //create a document session for every unit of work
    BeginRequest += (sender, args) => 
    {
      HttpContext.Current.Items[RavenSessionKey] = documentStore.OpenSession();
    }

    //Destroy the DocumentSession on EndRequest
    EndRequest += (o, eventArgs) =>
    {
        var disposable = 
                HttpContext.Current.Items[RavenSessionKey] as IDisposable;

        if (disposable != null)
            disposable.Dispose();
    };
}

//Getting the current DocumentSession
public static IDocumentSession CurrentSession
{
  get { return (IDocumentSession)HttpContext.Current.Items[RavenSessionKey]; }
}

我们现在要设置应用程序以支持多租户。我们希望有两个文档存储:一个用于通用目的,系统数据库,另一个用于当前(登录的)租户。

根据我们当前的设置,我们如何实现这一目标?

编辑:我们现在配置我们的应用程序如下:

我们在同一个 documentStore 上添加了 OpenSession(tenantid)BeginRequest(感谢 Ayende 下面的回答)

var tenant = HttpContext.Current.Request.Headers["Host"].Split('.')[0];
documentStore.DatabaseCommands.EnsureDatabaseExists(tenant);
HttpContext.Current.Items[RavenSessionKey] = 
                 documentStore.OpenSession(tenant);

因为我们将 Ninject 用于 DI,所以我们添加了以下绑定以确保我们使用的是正确的会话:

kernel.Bind<ISession>().To<Session>().WhenInjectedInto<UserService>();
kernel.Bind<ISession>().To<TenantSession>();

kernel.Bind<IDocumentSession>().ToMethod(ctx =>        
     MvcApplication.CurrentSession).WhenInjectedInto<Session>();

kernel.Bind<IDocumentSession>().ToMethod(ctx =>  
     MvcApplication.CurrentTenantSession).WhenInjectedInto<TenantSession>();

也许有更好的方法来使用 ravendb 和 mvc 配置多租户?

【问题讨论】:

    标签: asp.net-mvc-3 c#-4.0 ravendb multi-tenant


    【解决方案1】:

    安德鲁F,

    那么,您将有两个会话。一个是默认的 (OpenSession()),另一个是租户 (OpenSession(TenantId))

    【讨论】:

    • Tnx,但我们是在同一个DocumentStore 上调用OpenSession,还是我们还需要第二个DocumentStore
    • 不,一个文档存储就足够了。
    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    相关资源
    最近更新 更多