I have a case where each customer has his own DB – What i would like to do is for this to happen automatically eg when Autofac creates the controller it looks up the clientId and then gets the connection string
The following gets the string
public class ModuleContextProvider : IModuleContextProvider
{
IModuleContext context;
public IModuleContext GetContext(int moduleId)
{
if ( context == null)
{
//get module.
var rep = DependencyResolver.Current.GetService<IModuleRepository>();
var module = rep.GetById(moduleId);
context = GetContext(module);
}
return context;
}
public IModuleContext GetContext(Model.Module module)
{
if ( context == null)
{
//get module.
var rep = DependencyResolver.Current.GetService<IModuleRepository>();
context = new ModuleContext(module.DBConnection);
if ( context == null)
throw new InvalidOperationException("Could nto create DB COntext" );
}
return context;
}
}
but how do i get it into the Controllers so everyone does not need to worry about it each time , i have the following but obviously to create the repositry i need the context.
public CategoryController(ICommandBus commandBus, ICategoryRepository categoryRepository)
{
this.commandBus = commandBus;
this.categoryRepository = categoryRepository;
}
Is it possible to use a ROute eg //Controller/ModuleId/Method and then somehow get this from the Url ?
Thanks in advance for any help.
Edit: The real issue here I think is how to keep pushing the connection string around ( in this case in the form of the module) .
If anyone needs this ..
I stored it in the cookie and retrieve in the controller as follows