我有一個情況,其中每個客戶都有自己的數據庫 - 我想要做的就是自動發生這種情況,例如,當Autofac創建控制器時,它會查找的clientId然後獲取連接字符串ASP.NET MVC 3或4 - 如何處理多個數據庫
下得到的字符串
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;
}
}
但我怎麼把它搬進控制器所以大家並不需要每次都擔心,我有以下的但明顯爲了創建我需要的上下文。
public CategoryController(ICommandBus commandBus, ICategoryRepository categoryRepository)
{
this.commandBus = commandBus;
this.categoryRepository = categoryRepository;
}
是否可以使用路由如//控制器/的moduleId /方法,然後以某種方式從URL得到這個?
在此先感謝您的幫助。
編輯:真正的問題在這裏我認爲是如何繼續推動連接字符串(在這種情況下,以模塊的形式)。
這是類似於我ModuleContextProvider但它不回答如何工廠創建它。我需要像moduleId這樣的東西來告訴要使用的連接字符串。所以我可以看到的選項是 – 2011-12-21 23:37:54
1把「模塊」的值通過路由到某個地方,然後提取 - 不確定如何提取。 2.將它作爲參數傳遞給URL,再次不確定如何進入上述工廠3.引入會話狀態4.在每次方法調用時都通過它(非常難看) – 2011-12-21 23:41:19