NInject v3の新しいドキュメントと、 Factory Extensionの使用方法を読んだ後、コードがいたるところに例外をスローするため、まだ完全には理解できていないようです...
私はこの例外を受け取ります、人々がそれを望むなら私は全部を貼り付けることができます、しかし私は今のところそれを短くしようとします。
IDeployEntityContainerのアクティブ化中にエラーが発生しました一致するバインディングが利用できず、タイプは自己バインドできません。
これが私のコードです...NinjectBindModuleクラス
class MyNinjectModule : NinjectModule {
public override void Load() {
...
Bind<IDeployEntityFactory>().ToFactory();
Bind<IDeployEntityContainer>().To<DeployEntityContainer>();
...
}
}
工場を利用するクラス
class DeployController : IDeployController {
private readonly IDeployEntityFactory _entityFactory;
public DeployController(..., IDeployEntityFactory entityFactory) {
...
}
public void Execute() {
...
//I get the Exception on this line...
_entityFactory.GetDeployEntity<IDeployEntityContainer>();
...
}
}
ファクトリーインターフェース
public interface IDeployEntityFactory
{
T GetDeployEntity<T>();
}
工場での実装
public class DeployEntityFactory : IDeployEntityFactory
{
private readonly IResolutionRoot _resolutionRoot;
public DeployEntityFactory(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}
public T GetDeployEntity<T>()
{
return _resolutionRoot.Get<T>();
}
}
舞台裏では、Ninjectは、指定されたファクトリインターフェイスを実装するプロキシを作成し、すべてのメソッドをインターセプトして、プロキシが次のように動作するようにします...
工場内でオブジェクトを作成する際に特別なことやカスタムを行う必要がなければ、実際に自分で実装を作成する必要はないことを理解しています。
出典:http ://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/
編集1:
問題を確認するために必要なすべての情報を確実に残すために、DeployEntityContainerクラス/インターフェイスを追加しています
public abstract class DeployEntityBase : IDeployEntity
{
...
protected readonly IDeployEntityFactory _entityFactory;
protected DeployEntityBase(..., IDeployEntityFactory entityFactory)
{
...
_entityFactory = entityFactory;
...
}
...
}
public class DeployEntityContainer : DeployEntityBase, IDeployEntityContainer
{
...
public DeployEntityContainer(..., IDeployEntityFactory entityFactory)
: base(..., entityFactory)
{
}
}