博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过fsharp 使用Enterprise Library Unity 2
阅读量:4358 次
发布时间:2019-06-07

本文共 2133 字,大约阅读时间需要 7 分钟。

接着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
记录日志是最常见的行为注入。

这里最重要的是实现IIntercptionBehavior接口中的Invoke方法,这种方法有两个參数。第一个是被拦截的接口,第二个则是一个托付行为列表(以托付的形式给出)。

这里我们在列表循环前记录參数,在循环后记录返回值。

let result = getNext.Invoke().Invoke(input, getNext)
这句代码完毕详细的工作。

拦截的行为是依次进行的,彼此之际并无联系,也不应有联系。每一个拦截都仅关注本身的详细行为。这是最好的情况,只是实际中也并不能保证这一点。对拦截的顺序还是要多加注意。比方缓冲的样例,有可能截断注入行为列表。每一个行为对兴许的动作不可控,所以当缓冲须要做一些特殊操作直接返回值时,会忽略兴许的动作。又比方权限管理。

以下进行注冊,管理容器须要支持Interception

container.AddNewExtension
() |> ignore
注冊
container.RegisterType
(new Interceptor
(), new InterceptionBehavior
())let t = container.Resolve
();t.Msg()
能够看到当解析接口时就会跳出非常多记录。猜想应该在构造对象时做了不少的动作,父类的接口调用也会被记录。
最后是结果
From the logging interceptor: "Invoke method Void Msg():FSI_0002+TenantStore at 11:59:00"Hello, it's TenantStoreFrom the logging interceptor: "Method Void Msg() returned  at 11:59:00"
假设我们重复调用RegisterType多次。记录的条目也会对应增多。应该能够想象结构上的变化。
以上。

转载于:https://www.cnblogs.com/gccbuaa/p/7373434.html

你可能感兴趣的文章
凤姐讲学英语
查看>>
ActionBar
查看>>
5种方法实现数组去重
查看>>
2~15重点语法
查看>>
flask中的CBV,flash,Flask-Session,WTForms - MoudelForm,DBUtils 数据库连接池
查看>>
最近整理的提供免费代理列表的几个网站
查看>>
探偵ガリレオー転写る2
查看>>
快速排序算法C++实现[评注版]
查看>>
七尖记
查看>>
SAP(最短增广路算法) 最大流模板
查看>>
用极大化思想解决矩形问题学习笔记
查看>>
Django REST Framework 简单入门
查看>>
Hibernate中fetch和lazy介绍
查看>>
修改ip脚本
查看>>
解析xlsx与xls--使用2012poi.jar
查看>>
SSL-ZYC 活动安排
查看>>
c# 动态绘制直线和曲线
查看>>
Spring理解?
查看>>
[poj3261]Milk Patterns(后缀数组)
查看>>
[luogu3369]普通平衡树(fhq-treap模板)
查看>>