我正在查找使用和配置windsor以提供動態代理來攔截對另一個類的實例的調用的一些信息。在Windsor容器中使用DynamicProxy作爲修飾器模式
我的類代表一個資源,由於性能原因,該資源應該由容器保留爲長期存在的實例。但是,有時這種資源可能會轉變爲不可用狀態,並且需要更新。我想容器來處理這個,所以客戶端代碼不需要。我可以創建自己的工廠來做到這一點,我想知道是否有一些溫莎註冊涼爽,爲我做的,所以我沒有創建單獨的工廠類:)
這裏是一些僞代碼以演示問題:
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類的東西,以及如何配置容器使用它?謝謝!
使用城堡設施向前 – 2009-05-12 20:09:16