8

我一直在用Castle執行我的DI構建新的.NET解決方案。如何強制執行安裝程序的指令

它現在處於我想要控制安裝程序運行順序的階段。我已經構建了實現IWindsorInstaller來處理我的核心類型的單個類 - 例如IRepository,IMapper和IService等等。

我看到它的建議我實現我自己的InstallerFactory(猜測我只是重寫Select)在這個類中。

然後在我的電話使用這個新工廠:

FromAssembly.InDirectory(new AssemblyFilter("bin loca­tion")); 

我的問題 - 重寫保存方法時 - 什麼是強迫我安裝順序的最佳方式。

+0

手動添加安裝在您需要的順序?無論如何,你能告訴我們爲什麼你需要定義一個特定的訂單嗎? – 2011-06-15 13:49:26

+0

好吧,讓我們假設我想保證Service安裝程序首先運行(依賴關係圖允許)。該文檔說覆蓋InstallerFactory是要走的路。這個話題似乎很少涉及。我認爲最終歸結爲重新排序傳遞給Select方法的IEnumerable 2011-06-15 16:09:03

回答

21

我知道它已經解決了,但我無法找到如何真正實現任何例子InstallerFactory,所以這裏有一個解決方案,如果有人正在使用它。

使用方法:

[InstallerPriority(0)] 
    public class ImportantInstallerToRunFirst : IWindsorInstaller 
    { 
     public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) 
     { 
      // do registrations 
     } 
    } 

只是一個優先添加InstallerPriority屬性的「安裝順序敏感」類別。安裝人員將按升序排序。沒有優先安裝程序將默認爲100

如何實現:

public class WindsorBootstrap : InstallerFactory 
    { 

     public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes) 
     { 
      var retval = installerTypes.OrderBy(x => this.GetPriority(x)); 
      return retval; 
     } 

     private int GetPriority(Type type) 
     { 
      var attribute = type.GetCustomAttributes(typeof(InstallerPriorityAttribute), false).FirstOrDefault() as InstallerPriorityAttribute; 
      return attribute != null ? attribute.Priority : InstallerPriorityAttribute.DefaultPriority; 
     } 

    } 


[AttributeUsage(AttributeTargets.Class)] 
    public sealed class InstallerPriorityAttribute : Attribute 
    { 
     public const int DefaultPriority = 100; 

     public int Priority { get; private set; } 
     public InstallerPriorityAttribute(int priority) 
     { 
      this.Priority = priority; 
     } 
    } 

當啓動應用程序時,Global.asax的等:

container.Install(FromAssembly.This(new WindsorBootstrap())); 
+1

偉大的解決方案,謝謝! – 2012-04-18 09:11:45

+1

這是應該肯定合併到溫莎的那種延伸點。做得好。 – M05Pr1mty 2013-03-19 10:21:13

+1

有趣的是,如果您正在註冊來自不同程序集的安裝程序,則此方法不起作用。安裝程序工廠每個程序集調用一次。 – ScottG 2015-01-26 09:34:27

0

在最後,我不得不使用InstallerFactory和執行的順序規則,預先用我特定的順序返回IEnumerable<Type>建議

+0

您是否有鏈接到一些關於InstallerFactory的更多信息? – Chev 2012-01-28 06:16:25

+0

@Chev我沒有發現太多的關於這方面的文章,我只是不得不使用試驗和錯誤來獲得我期待的結果。 – 2012-01-30 14:47:37

3

你可以打電話給你的安裝順序,他們需要在Global.asax.cs中實例化或例如在從Global.asax.cs調用的Bootstrapper類中。

 IWindsorContainer container = new WindsorContainer() 
      .Install(     
       new LoggerInstaller()   // No dependencies 
       , new PersistenceInstaller() // --""-- 
       , new RepositoriesInstaller() // Depends on Persistence 
       , new ServicesInstaller()  // Depends on Repositories 
       , new ControllersInstaller() // Depends on Services 
      ); 

他們被實例化的順序,你可以後添加斷點,並檢查容器"Potentially misconfigured components"

如果有,請檢查它們的Status - >details,如果沒有,則它是正確的順序。

該解決方案既快速又簡單,文檔中提到使用InstallerFactory類來更加嚴格地控制安裝程序,所以如果您有大量的安裝程序,那麼其他解決方案可能會更好。 (使用代碼作爲公約不應該要求安裝噸的?)

http://docs.castleproject.org/Windsor.Installers.ashx#codeInstallerFactorycode_class_4

+0

這確實可行,但是在導致這個問題的解決方案中,我們在庫中引用了未知的安裝程序,並且我們希望確保它們都會根據我們的約定被選中,但是按照特定的順序運行 - 也是基於約定。 – 2013-06-03 15:47:05

相關問題