別のクラスのインスタンスへの呼び出しをインターセプトする動的プロキシを提供するためのウィンザーの使用と構成に関する情報を探しています。
私のクラスは、パフォーマンス上の理由から、コンテナによって長寿命のインスタンスとして保持される必要があるリソースを表しています。ただし、このリソースは使用できない状態に移行する場合があり、更新が必要になります。クライアント コードで処理する必要がないように、コンテナーでこれを処理する必要があります。これを行うために独自のファクトリを作成できます。別のファクトリ クラスを作成する必要がないので、Windsor 登録のクールネスがあるかどうかを知りたいです :)
問題を示す擬似コードを次に示します。
public interface IVeryImportantResource
{
void SomeOperation();
}
public class RealResource : IVeryImportantResource
{
public bool Corrupt { get; set; }
public void SomeOperation()
{
//do some real implementation
}
}
public class RealResourceInterceptor : IInterceptor
{
private readonly IKernel kernel;
public RealResourceInterceptor(IKernel Kernel)
{
kernel = Kernel;
}
public void Intercept(IInvocation invocation)
{
RealResource resource = invocation.InvocationTarget as RealResource;
if(resource.Corrupt)
{
//tidy up this instance, as it is corrupt
kernel.ReleaseComponent(resource);
RealResource newResource = kernel.Resolve<RealResource>(); //get a new one
//now what i would like to happen is something like this
//but this property has no setter, so this doesn't work
//also, i would like to know how to register RealResourceInterceptor as well RealResourceInterceptor
invocation.InvocationTarget = newResource;
}
invocation.Proceed();
}
}
私の RealResourceInterceptor クラスのようなものを実装する方法と、それを使用するようにコンテナを構成する方法はありますか? ありがとう!