1

ninject.extensions.conventions特定のアセンブリ内のすべての実装をバインドし、バインディングのメタデータとしてアセンブリ名でタグ付けするために使用しています。Get を使用してこれらのアイテムを引き出すことができ、標準として func を提供します。

私が知りたいのは、この関数が解決されたすべての子にも適用されるということですか? 私の心配は、私のロジックは今は機能していますが、子供たちを満たすバインディングを複数回追加すると、ninject がスローされることです。

コードサンプル:

_kernel.Bind(binder => binder.From(new[] { pathToAssembly })
                             .SelectAllClasses()
                             .BindAllInterfaces()
                             .Configure(binding => 
                                        binding.WithMetadata("context", 
                                                             assemblyName)));


 _kernel.Get<IRootDependency>
         (metadata => metadata.Get<IRootDependency>("context") == 
                                                   assemblyName);

// Bound from convention above.
RootDependencyBase: IRootDependency
{
  Public RootDependencyBase(IChildDependency Child) {};
}

// Bound using the convention above with the same MetaData Tag.
ChildDependencyFromSameAssembly : IChildDependency {}

// Bound using a differing convention and does not have the same MetaData tag.
ChildDependencyFromOtherAssembly : IChildDependency {}

上記のサンプルに基づいて、メタデータ フィルターに基づいて IRootDependency が正しいバインディングに解決されることがわかります。

私が見つけようとしているのは、次の真実です。

このフィルターは、依存関係チェーンをフィードしません。IChildDependency は例外をスローします。これは、バインディングで MetaData が指定されていてもクエリされないためです。

4

2 に答える 2

4

制約は、ルートの解決にのみ適用されます。子の依存関係を含む複数のアセンブリがある場合、例外が発生します。

それを機能させるには、バインディングに条件を追加する必要があります。たとえば、次のようにします。

.When(r => r.ParentContext == null || r.ParentContext.Binding.Metadata.Get<string>("context", null) == assemblyName)

または、ルート リクエスト (parentRequest が null になるまで request.ParentRequest) を取得し、制約を適用します。

.When(r => GetRootRequest(r).Constraint(r))
于 2013-01-14T14:31:33.700 に答える
2

はい、あなたの例がIChildDependency同じアセンブリに別の実装を持っている場合、メッセージでChildDependencyFromSameAssembly例外が発生ActivationExceptionします:

Error activating IDependency
More than one matching bindings are available.

同じアセンブリからのどの実装がIChildDependencyより適しているかを見つけるために、Ninjectに正確な基準を提供する必要があります

于 2013-01-14T14:32:36.220 に答える