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:
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}
Is this method of connecting to several identical databases valid or is it better practice to construct a sessionfactory foreach customer database?
NHibernate is the ORM, an object-relational platform that works on the ASP.Net core. Therefore, we can easily deploy the core project developed in ASP.NET that uses NHibernate on the Linux server without any effort. The application works in the same way as it will do on a windows platform.
NHibernate has been my favorite ORM for long time. Although it's more complex for beginners than Entity Framework it's more matured and many developers consider it to be practially an industry standard. NHibernate works with ASP.NET Core too.
NHibernate 5.3 was released on July 19, 2020.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With