문제

We are currently developing an application based on NHibernate and ASP.NET MVC and a SQL Server backend. Since I'm fairly new to NHibernate, I'm tryig to understand best practices.

Our application requires every user to have it's own SQL Server database. These databases all have an identical structure.

Our customers are identified with a customercode, e.g. 1500.

We've come up with a custom connection provider for nHibernate, which we already use in our nServiceBus backend services:

public class DynamicConnectionProvider : DriverConnectionProvider
{
    public override IDbConnection GetConnection()
    {
        IDbConnection conn = Driver.CreateConnection();

        try
        {
            var messageExecutionContext = ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext;
            if (messageExecutionContext.CustomerId == 0)
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["dev"]
                    .ConnectionString;
            }
            else
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["default"]
                    .ConnectionString
                    .FormatWith(messageExecutionContext.CustomerId);
            }

            conn.Open();
        }
        catch (Exception)
        {
            conn.Dispose();
            throw;
        }
        return conn;
    }
}

This connection provider checks the customer code in a context object and sets the connectionstring accordingly.

We are planning to provide a HttpContext aware ITTTContextProvider. For this I have two questions:

  1. How can we retrieve the customer code from the url and put it into our context object for every request? when we use the following route?

    <main-site-url>/{customercode}/{controller}/{action}/{id}

  2. Is this method of connecting to several identical databases valid or is it better practice to construct a sessionfactory foreach customer database?

도움이 되었습니까?

해결책

In order to get the customercode you need to access the route data, something along the lines of

HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current); //ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext; 
RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);  
var cusomterCode = routeData.Values["customercode"]

My second suggestion would be not to put this code in the above snippet provided. Abstract it away. See Joshua's answer which highlights the approach I am thinking of.

Can't really help on the second question, actually not familiar with both frameworks mentioned.

다른 팁

See my recent blog post which shows how to use a subdomain to connect to different databases, although it would be easy to implement your own version of ITenantContext that grabbed the customer code from the request url. Also uses separate session factories for each tenant.

http://www.yellowfeather.co.uk/2011/01/multi-tenancy-on-sharp-architecture/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top