标签:enterprise library fsharp f# unity interceptor
接着Depandency Injection继续。最想做的还是用现成的程序模块对程序进行行为注入。不过不急,在此之前自己写一个接口对象观察一下IInterceptionBehavior接口的功效。type LogingInterceptionBehavior() = let WriteLog message = printfn "From the logging interceptor: %A" message interface IInterceptionBehavior with member x.Invoke((input:IMethodInvocation), (getNext:GetNextInterceptionBehaviorDelegate)) = String.Format("Invoke method {0}:{2} at {1}", input.MethodBase, DateTime.Now.ToLongTimeString(), input.Target) |> WriteLog let result = getNext.Invoke().Invoke(input, getNext) match result.Exception with | null -> String.Format("Method {0}:{3} returned {1} at {2}", input.MethodBase, result.ReturnValue, DateTime.Now.ToLongTimeString(), input.target) |> WriteLog | _ -> String.Format("Method {0} threw exception {1} at {2}", input.MethodBase, result.Exception.Message, DateTime.Now.ToLongTimeString()) |> WriteLog result member x.GetRequiredInterfaces() = Type.EmptyTypes |> Seq.ofArray member x.WillExecute with get() = true
container.AddNewExtension<Interception>() |> ignore
container.RegisterType<ITenantStore, TenantStore>(new Interceptor<TransparentProxyInterceptor>(), new InterceptionBehavior<LogingInterceptionBehavior>()) let t = container.Resolve<ITenantStore>(); t.Msg()可以看到当解析接口时就会跳出很多记录,猜想应该在构造对象时做了不少的动作,父类的接口调用也会被记录。
From the logging interceptor: "Invoke method Void Msg():FSI_0002+TenantStore at 11:59:00" Hello, it's TenantStore From the logging interceptor: "Method Void Msg() returned at 11:59:00"
通过fsharp 使用Enterprise Library Unity 2
标签:enterprise library fsharp f# unity interceptor
原文地址:http://blog.csdn.net/samwell/article/details/40584685