Hi I’ve just completed a major refactor so we can implement a Multitenancy OrmLite AuthProvider properly without relying on the Request Context Singleton (which only works in ASP.NET). Now all access to IAuthRepository
goes through HostContext.AppHost.GetAuthRepository(IRequest)
which similar to the other dependencies can be overridden.
One of the things we need to be mindful of is when OrmLite AuthRepository gets called on startup to create the schema, it doesn’t have a IRequest
context and in this case needs to go through and create the schema for all Multitenancy db’s.
Whilst when it’s called at runtime it does have access to the IRequest
and can just use the current db connection.
So to handle this I’ve created a new OrmLiteAuthRepositoryMultitenancy
class with 2 constructors, with the default registration getting called on startup which should be configured with all the connection strings of all your Multitenancy DB’s that you want it to create scheams for, e.g:
var connStrings = new [] {
"company1ConnectionString",
"company2ConnectionString",
//etc
}
container.Register<IAuthRepository>(c =>
new OrmLiteAuthRepositoryMultitenancy(c.Resolve<IDbConnectionFactory>(), connStrings)
{
UseDistinctRoleTables = true,
});
You can just pass in an empty array if you’ve already created schema’s for all db’s and don’t want to check/create them.
Then to handle the normal use-case of getting called at runtime, you can get it to use the current Multitenancy DB connection at runtime by overriding GetAuthRepository()
in your AppHost, e.g:
public override IAuthRepository GetAuthRepository(IRequest req = null)
{
return req != null
? new OrmLiteAuthRepositoryMultitenancy(GetDbConnection(req))
: TryResolve<IAuthRepository>();
}
So now when GetAuthRepository()
is called within the context of a request it uses the same Multitenancy DB as your other services, otherwise when its called outside, it uses the default DB registration (which can only be called to initialize the schemas).
This change is now available from v4.0.61 that’s now on MyGet. I would appreciate it if you can look at using this new support support for Multitenancy DB AuthRepository and let me know if it does what you need, thanks.