2011-11-25 52 views
1

我需要爲ninject模塊提供一些全局範圍,並且指出最好通過模塊注入它。ninject模塊的參數

我正在掃描程序集的模塊,因此,無法確定如何提供這些參數(乾淨)。

我有一個控制檯應用程序,我想選擇基於應用程序參數的類型的實現。

問題是這些類型具有依賴於這些參數的特定反序列化,這是在查找時間確定的。

我希望做的是這樣的:

public class Module : MyNinjectModule<Module> 
{ 
    private enum FictionalEnum 
    { 
     FirstType, 
     SecondType 
    } 

    private string[] _args; 

    private FictionalEnum Type 
    { 
     get 
     { 
      return IsFirstType(_args) 
         ? FictionalEnum.FirstType 
         : FictionalEnum.SecondType; 
     } 
    } 

    public Module(string[] rawArgs) 
    { 
     _args = rawArgs; 
    } 

    protected override void LoadCustomBindings() 
    { 
     Bind<IBaseType>().To<FirstImpl>() 
      .When(req => this.Type == FictionalEnum.FirstType); 

     Bind<IBaseType>().To<SecondImpl>() 
      .When(req => this.Type == FictionalEnum.SecondType); 
    } 
} 

有沒有人有這方面的任何建議嗎?

回答

1

在我看來,這個邏輯不屬於一個模塊。我要麼注入所有實例,然後在應用程序中決定採用哪個實例或注入工廠,並根據參數讓工廠創建正確的類型。