ファクトリを使用してデータセンダーを返しています。
Bind<IDataSenderFactory>()
.ToFactory();
public interface IDataSenderFactory
{
IDataSender CreateDataSender(Connection connection);
}
私は、異なるタイプを取る datasender (WCF とリモーティング) の 2 つの異なる実装を持っています。
public abstract class Connection
{
public string ServerName { get; set; }
}
public class WcfConnection : Connection
{
// specificProperties etc.
}
public class RemotingConnection : Connection
{
// specificProperties etc.
}
パラメータから渡された Connection のタイプに基づいて、Ninject を使用してこれらの特定のタイプのデータセンダをバインドしようとしています。私は次のことを試みましたが失敗しました:
Bind<IDataSender>()
.To<RemotingDataSender>()
.When(a => a.Parameters.Single(b => b.Name == "connection") as RemotingConnection != null)
これは、「.When」が要求のみを提供し、実際のパラメーター値を取得してその型を確認できるように完全なコンテキストが必要になるためだと思います。名前付きバインディングを使用する以外に、実際にファクトリを実装してそこにロジックを配置する以外に、何をすべきか途方に暮れています。
public IDataSender CreateDataSender(Connection connection)
{
if (connection.GetType() == typeof(WcfConnection))
{
return resolutionRoot.Get<IDataSender>("wcfdatasender", new ConstructorArgument("connection", connection));
}
return resolutionRoot.Get<IDataSender>("remotingdatasender", new ConstructorArgument("connection", connection));
}