2014-11-04 129 views
0

我有一個MVC應用程序,我正在使用Autofac解決依賴關係。Autofac註冊多個容器

我有一種情況,我必須創建2個容器,運行時應根據條件決定使用哪個容器。

條件是如果控制器Home被調用,我需要使用container1,否則我必須使用container2。

Application_Start是我註冊容器的地方。

我不知道如何在運行時發生這種情況。任何幫助,高度讚賞。

感謝來自不同的容器,讓控制器決心

+0

容器之間有多少註冊不同?有一個委託工廠或其他一些方式來改變運行時實例化可能更有意義 – ESG 2014-11-04 01:25:19

+0

一些註冊有所不同,如果你可以幫我解決一些代碼示例的問題,那將是非常好的。 – user2388339 2014-11-04 01:40:02

+0

我建議查看Autofac文檔的[選擇實施者](https://github.com/autofac/Autofac/wiki/Component-Creation)部分。您還應該更好地解釋您首先要做的事情,而不是詢問具體實施的細節。 – ESG 2014-11-04 01:55:35

回答

1

的一個原因是,如果你的應用程序由多個獨立的模塊。在這種情況下,您可能不希望模塊互相影響,並且每個模塊都有一個容器是合理的。然而,在幾乎所有其他情況下,擁有多個容器實例都沒有意義。

所以,如果你需要這個,你需要建立自己的定製IControllerFactory,可以根據控制器類型切換容器。舉例來說,這樣的事情:

internal sealed class MultiplContainerControllerFactory : IControllerFactory 
{ 
    public IController CreateController(RequestContext requestContext, string controllerName) 
    { 
     var factory = this.GetFactory(requestContext); 
     var controller = factory.CreateController(requestContext, controllerName); 

     // By storing the factory in the request items for this controller, 
     // we allow it to be easily retrieved 
     // during ReleaseController and delegate releasing to the correct controller. 
     HttpContext.Current.Items["ContrFct_" + controller.GetType().FullName] = factory; 

     return controller; 
    } 

    public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, 
     string controllerName) 
    { 
     var factory = this.GetFactory(requestContext); 
     return factory.GetControllerSessionBehavior(requestContext, controllerName); 
    } 

    public void ReleaseController(IController controller) 
    { 
     var controllerFactory = (IControllerFactory)HttpContext.Current.Items["ContrFct_" + 
      controller.GetType().FullName]; 

     controllerFactory.ReleaseController(controller); 
    } 

    private IControllerFactory GetFactory(RequestContext context) 
    { 
     // return the module specific factory based on the requestcontext 
    } 
} 

除此之外,您將需要有每個集裝箱特殊AutofacControllerFactory。這可能看起來像這樣:

public sealed class AutofacControllerFactory : DefaultControllerFactory 
{ 
    public readonly IContainer Container; 
    private readonly string moduleName; 

    public AutofacControllerFactory(IContainer container, string moduleName) 
    { 
     this.Container = container; 
     this.moduleName = moduleName; 
    } 

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     if (controllerType == null) 
     { 
      // The base method throws an expressive 404 HTTP error. 
      base.GetControllerInstance(requestContext, controllerType); 
     } 

     // We need to start a new lifetime scope when resolving a controller. 
     // NOTE: We can apply MatchingScopeLifetimeTags.RequestLifetimeScopeTag to the BeginLifetimeScope 
     // method and in this case we can use .InstancePerRequest(), but in that case it becomes impossible to 
     // verify the DI configuration in an integration test. 
     ILifetimeScope lifetimeScope = this.Container.BeginLifetimeScope(); 

     // We need to store this lifetime scope during the request to allow to retrieve it when the controller 
     // is released and to allow to dispose the scope. Memory leaks will be ensured if we don't do this. 
     HttpContext.Current.Items[controllerType.FullName + "_lifetimeScope"] = lifetimeScope; 

     // This call will throw an exception when we start making registrations with .InstancePerRequest, 
     // because the WebRequest functionality of Autofac is tied to the AutofacDependencyResolver, which we 
     // don't use here. We can't use the AutofacDependencyResolver here, since it stores the created lifetime 
     // scope in the HttpContext.Items, but it uses a fixed key, which means that if we resolve multiple 
     // controllers for different application modules, they will all reuse the same lifetime scope, while 
     // this scope originates from a single container. 
     try 
     { 
      return (IController)lifetimeScope.Resolve(controllerType); 
     } 
     catch (Exception ex) 
     { 
      lifetimeScope.Dispose(); 

      throw new InvalidOperationException("The container of module '" + this.moduleName + 
       "' failed to resolve controller " + controllerType.FullName + ". " + ex.Message, ex); 
     } 
    } 

    [DebuggerStepThrough] 
    public override void ReleaseController(IController controller) 
    { 
     try 
     { 
      base.ReleaseController(controller); 
     } 
     finally 
     { 
      var scope = (ILifetimeScope)HttpContext.Current 
       .Items[controller.GetType().FullName + "_lifetimeScope"]; 

      scope.Dispose(); 
     } 
    } 
}