通過fsharp 使用Enterprise Library Unity 3 - 三種攔截模式的探索
來源:程序員人生 發布時間:2014-11-04 08:43:47 閱讀次數:3683次
這篇就3種攔截模式進行1下探索。
特性總結
|
類型 |
特點 |
其他 |
InterfaceInterceptor |
Innstance |
僅單接口 |
類內部函數相互援用也能沒法引發攔截行動 |
TransparentProxyInterceptor |
Instance |
多接口(接口之間可以切換) MarshalByRef 運行緩慢 接口類型(virtual, non-virtual, or interface) |
類內部函數相互援用也能沒法引發攔截行動 |
VirtualMethodInterceptor |
Type |
多接口 不能用在已有對象上,接口函數必須為virtual |
類內部函數相互援用也能引發攔截行動 |
回想1下類的聲明,兩個接口,1個實現。fsharp實現接口函數的方法與csharp其實不完全1致,會造成1些實現上的困擾,這在后面會提到。
type ITenantStore =
abstract member Msg : unit->unit
type TenantStore() as x=
//do printfn "new TenantStore %A" (x.GetHashCode())
interface ITenantStore with
member this.Msg() = printfn "Hello, it's TenantStore"
interface IDisposable with
member this.Dispose() = printfn "TenantStore hase been cleaned"
下面進行1些測試。
let showregistrations (container:UnityContainer) =
container.Registrations |> Seq.iter (fun i -> printfn "Regist Type:%A" i.RegisteredType)
using(new UnityContainer())(fun ctner->
ctner.AddNewExtension<Interception>() |> ignore
ctner.RegisterType<ITenantStore, TenantStore>(new Interceptor<TransparentProxyInterceptor>(),//后續對此注入策略進行切換
new InterceptionBehavior<LogingInterceptionBehavior>()) |> ignore
showregistrations ctner
let t = ctner.Resolve<ITenantStore>()
t.Msg())
showregistrations 顯示已注冊的類型,用來展現注冊內部的信息。另外,在交互式代碼中使用using相較于use更加的適合,FSI聲明的對象的生命周期是全局的,在做實驗的時候沒法控制container,反復清環境不太方便。
TransparentProxyInterceptor的結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
From the logging interceptor: "Invoke method System.Type GetType():System.Object at 16:20:45"
From the logging interceptor: "Method System.Type GetType():System.Object returned FSI_0002+TenantStore at 16:20:45"
From the logging interceptor: "Invoke method Void Msg():FSI_0002+ITenantStore at 16:20:45"
Hello, it's TenantStore
From the logging interceptor: "Method Void Msg():FSI_0002+ITenantStore returned at 16:20:45"
val it : unit = ()
InterfaceInterceptor的結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
From the logging interceptor: "Invoke method Void Msg():FSI_0002+ITenantStore at 16:22:54"
Hello, it's TenantStore
From the logging interceptor: "Method Void Msg():FSI_0002+ITenantStore returned at 16:22:54"
val it : unit = ()
雖然對象有兩個接口,IDiposable接口和ITenantStore接口,攔截依然成功了,所以支持多對象指的是接口之間的切換,并不是不支持實現多對象的類。
VirtualMethodInterceptor的結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
Hello, it's TenantStore
val it : unit = ()
攔截失敗。 研究后發覺是由于Fsharp中其實不直接支持Virtual方法,Fsharp實現接口成員,默許為顯示(explicit)實現,想要申明Virtual方法需要1些技能。
可參考http://cs.hubfs.net/topic/None/73936
再定義1個測試類來試試VirtualMethodInterceptor。
type testClass() =
abstract member Test : unit -> unit
default x.Test() =
printfn "hello "
using(new UnityContainer())(fun ctner->
ctner.AddNewExtension<Interception>() |> ignore
ctner.RegisterType<testClass>(new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<LogingInterceptionBehavior>()) |> ignore
showregistrations ctner
let t = ctner.Resolve<testClass>()
t.Test()
)
結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+testClass
From the logging interceptor: "Invoke method Void Test():FSI_0002+testClass at 9:30:27"
hello
From the logging interceptor: "Method Void Test():FSI_0002+testClass returned at 9:30:27"
val it : unit = ()
攔截成功。
下面略加改寫原來的類定義。使接口都以virtural的情勢暴露出來
type TenantStore() as x=
//do printfn "new TenantStore %A" (x.GetHashCode())
abstract Msg : unit -> unit
default x.Msg() =
printfn "Hello, it's TenantStore"
abstract Dispose : unit -> unit
default x.Dispose() =
printfn "TenantStore hase been cleaned"
interface ITenantStore with
member x.Msg() = x.Msg()
interface IDisposable with
member x.Dispose() = x.Dispose()
有些繁瑣,接口實現的代碼可以復用,不幸中的萬幸。下面的例子中我同時測試1下多接口之間轉換的情況,看看VirtualMethodInterceptor是不是也支持多接口,文檔上沒有明確表明。
測試代碼
using(new UnityContainer())(fun ctner->
ctner.AddNewExtension<Interception>() |> ignore
ctner.RegisterType<ITenantStore, TenantStore>(new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<LogingInterceptionBehavior>()) |> ignore
ctner.RegisterType<testClass>(new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<LogingInterceptionBehavior>()) |> ignore
showregistrations ctner
let t = ctner.Resolve<ITenantStore>()
t.Msg()
let o = (box t) :?> IDisposable
o.Dispose()
)
TransparentProxyInterceptor
成功 結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
From the logging interceptor: "Invoke method System.Type GetType():System.Object at 9:38:47"
From the logging interceptor: "Method System.Type GetType():System.Object returned FSI_0010+TenantStore at 9:38:47"
From the logging interceptor: "Invoke method Void Msg():FSI_0002+ITenantStore at 9:38:47"
Hello, it's TenantStore
From the logging interceptor: "Method Void Msg():FSI_0002+ITenantStore returned at 9:38:47"
From the logging interceptor: "Invoke method System.Type GetType():System.Object at 9:38:47"
From the logging interceptor: "Method System.Type GetType():System.Object returned FSI_0010+TenantStore at 9:38:47"
From the logging interceptor: "Invoke method Void Dispose():System.IDisposable at 9:38:47"
TenantStore hase been cleaned
From the logging interceptor: "Method Void Dispose():System.IDisposable returned at 9:38:47"
val it : unit = ()
InterfaceInterceptor
失敗 結果:
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
From the logging interceptor: "Invoke method Void Msg():FSI_0002+ITenantStore at 9:39:44"
Hello, it's TenantStore
From the logging interceptor: "Method Void Msg():FSI_0002+ITenantStore returned at 9:39:44"
System.InvalidCastException: 沒法將類型為“DynamicModule.ns.Wrapped_ITenantStore_67632c824c8e42bbad5925d203ac819b”的對象強迫轉換為類型“System.IDisposable”。
在 FSI_0015.it@149⑺.Invoke(UnityContainer ctner) 位置 E:WorkHellfsharp-practiseEnterpriseLibraryUnityProgram.fs:行號 157
在 Microsoft.FSharp.Core.Operators.Using[T,TResult](T resource, FSharpFunc`2 action)
在 <StartupCode$FSI_0015>.$FSI_0015.main@()
已因出錯而停止
VirtualMethodInterceptor
成功 結果
Regist Type:Microsoft.Practices.Unity.IUnityContainer
Regist Type:Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy
Regist Type:FSI_0002+ITenantStore
Regist Type:FSI_0002+testClass
From the logging interceptor: "Invoke method Void Msg():FSI_0017+TenantStore at 9:42:01"
Hello, it's TenantStore
From the logging interceptor: "Method Void Msg():FSI_0017+TenantStore returned at 9:42:01"
From the logging interceptor: "Invoke method Void Dispose():FSI_0017+TenantStore at 9:42:01"
TenantStore hase been cleaned
From the logging interceptor: "Method Void Dispose():FSI_0017+TenantStore returned at 9:42:01"
val it : unit = ()
通過結果可以看到TransparentProxyInterceptor在構建的進程中多了1些步驟,翻了1下幫助文檔。說這個代理是使用 TransparentProxy/RealProxy infrastructure生成的。因此速度最慢。而另兩個則是通過動態代碼生成的(這是甚么?反射?)。
這里可以有個小的結論了,InterfaceInterceptor最小巧實用,其他兩個或多或少在使用上都要留意1下各自的特性。
再看看可替換性。
(box t) :?> TenantStore
結果
TransparentInterceptor 失敗
VirtualMethodInterceptor 成功
InterfaceInterceptor 失敗 和料想的1致
以上
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈