2016-09-01 10 views
1

目的は次のようにカーネルを設定するときに、同じコンストラクタ引数を複数回宣言することを避けるためにです:NInject:複数のバインディング間でコンストラクタ宣言を共有

Kernel.Bind<ISomeService>().To<SomeService>(); 
Kernel.Bind<SomeService>().ToSelf().WithConstructorArgument("arg", "value"); 
:で試してみました

Kernel.Bind<ISomeService>().To<SomeService>() 
    .WithConstructorArgument("arg", "value"); 
Kernel.Bind<SomeService>.ToSelf() 
    .WithConstructorArgument("arg", "value"); 

"バインドチェーン"が考慮されることを望んでいますが、そうではありません。

答えて

1

次の例のように、種類のあなたの引数をパックすることができます:

public class FooConfig 
{ 
    private readonly string value; 

    public FooConfig(string value) 
    { 
     this.value = value; 
    } 

    public Value 
    { 
     get { return this.value; } 
    } 
} 

を、次にようにそれをバインドします

Bind<FooConfig>().ToConstant(new FooConfig("configValue")); 

、その後FooConfigタイプが注入持っている依存関係を適応させます。

+0

あなたはこの質問を見ることができると思いますか? http://stackoverflow.com/questions/40198294/how-to-configure-ninject-so-that-it-creates-a-single-instance-per-controller?noredirect=1#comment67673663_40198294 – eddy

関連する問題