2012-03-01 14 views
8

के साथ एक MVC 3 आवेदन के भीतर सेटअप multitenancy को हम एक mvc3 आवेदन सेटअप RavenDb साथ निम्नलिखित तरीके से किया है (NoSql with RavenDb and Asp.net MVC से कुछ मदद के साथ):कैसे ravendb

निम्नलिखित कोड 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]; } 
} 

अब हम multitenancy का समर्थन करने के लिए आवेदन सेटअप करना चाहते हैं। हम दो दस्तावेज़ों को रखना चाहते हैं: एक सामान्य उद्देश्य के लिए, सिस्टम डेटाबेस और वर्तमान (लॉग इन) टैनेंट के लिए एक।

हमारे वर्तमान सेटअप के आधार पर हम इसे कैसे प्राप्त करने के बारे में जाते हैं?

संपादित:

हम एक ही documentStore पर BeginRequest को OpenSession(tenantid) जोड़ा (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 के साथ multitenancy को कॉन्फ़िगर करने का एक बेहतर तरीका है?

उत्तर

5

एंड्रयूएफ,

आपके पास दो सत्र होने जा रहे हैं। एक जो defalt (OpenSession()) और दूसरा है जो किरायेदार के लिए है (OpenSession(TenantId))

+2

टीएनएक्स, लेकिन क्या हम उसी 'दस्तावेज़स्टोर' पर 'ओपनसेशन' कहते हैं या क्या हमें दूसरी 'दस्तावेज़स्टोर' की भी आवश्यकता है? –

+3

नहीं, एक दस्तावेज़ स्टोर पर्याप्त है। –

 संबंधित मुद्दे