2

我正在查找使用和配置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類的東西,以及如何配置容器使用它?謝謝!

回答

2

這個問題更多的是關於更新單個組件而不是攔截。更新單身人士的問題在this question中得到解答。底線:看起來並不容易,有許多這種做法的陷阱。

也許問題就出在這部分得到破壞(爲什麼會發生這種情況?)

+1

使用城堡設施向前 – 2009-05-12 20:09:16

0

我的對象是一個WCF代理。這些對象將轉換爲故障狀態,導致它們不可用。我無法控制何時或是否會過渡。我只能檢測到它發生了,並重新創建一個新的代理。

感謝您的鏈接,下面引用的部分描述了大致如何我目前做的:

或者另一種方法是 有一個裝飾你的容器用 單身生活方式註冊服務 ,但是您的實際 基本服務在 容器中註冊了一個瞬態生活方式 - 然後當您需要刷新 組件時,只需處置 裝飾,並用新鮮 解決實例來替換它(使用的關鍵部件,而不是 服務,以避免使 裝飾解決它 ) - 這避免了與其他 單服務問題(不是 是從持有到 陳舊的服務已處置使它們無法使用的 「刷新」),但 做的,而不是任一需要一點鑄造等方面 的使其工作

我的問題是, :

1)使用靜態類型的裝飾器來包裝我的RealResource(如上)。一旦你得到了這些代理的數量,它成爲一個痛苦,必須爲他們創建裝飾器。

2)使用工廠對象創建動態代理來管理任何WCF代理的狀態。2)遠遠超過了溫莎,但似乎是溫莎可能已經能夠爲我做的一件事。所以,我想知道是否有任何容器automagic,這將允許我在配置時註冊一個攔截器,而不是創建自己的工廠?

事情是這樣的僞代碼:

container.AddComponent("dynamicProxyWrapper", typeof(IRealResource), typeof(RealResource)).UsingInterceptor(typeof(RealResourceInterceptor)); 
+0

您是否嘗試過的WCF設施的方式嗎? http://www.castleproject.org/container/facilities/trunk/wcf/index.html – 2009-02-17 11:04:40