接着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<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"

假设我们重复调用RegisterType多次。记录的条目也会对应增多。应该能够想象结构上的变化。





以上。

通过fsharp 使用Enterprise Library Unity 2的更多相关文章

  1. 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索

    这篇就三种拦截模式进行一下探索. 特性总结   类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentPr ...

  2. 通过Fsharp探索Enterprise Library Exception

    Exception怎么生成是一回事,怎么展示又是还有一回事了. Exception Block主要关注的点在于Exception信息的展示.Exception不同于一般的log信息,是系统设计者未考虑 ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception 依赖注入容器Uni ...

  4. 微软Enterprise Library 4.1和Unity 1.2

    说明 微软模式与实践团队今天发布了Enterprise Library 4.1和Unity 1.2版本,这次发布的主要新特性如下: 1. 支持Visual Studio 2008 SP1 2. Uni ...

  5. Enterprise Library系列文章目录(转载)

    1. Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (初级) 2. Microsoft Enterprise Lib ...

  6. Enterprise Library 5.0 系列教程

    1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...

  7. 微软企业库5.0 学习之路——扩展学习篇、库中的依赖关系注入(重构 Microsoft Enterprise Library)[转]

    这篇文章是我在patterns & practices看到的一篇有关EntLib5.0的文章,主要介绍了EntLib5.0的这次的架构变化由来,觉得很不错,大家可以看一下! 在过去几年中,依赖 ...

  8. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  9. 基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作

    由于一个客户朋友的需求,需要我的Winform开发框架支持国产达梦数据库的操作,这个数据库很早就听过,但是真正一般项目用的很少,一般在一些特殊的项目可能需要用到.由于我的Winform开发框架,是基于 ...

随机推荐

  1. IIS、Asp.net 编译时的临时文件路径

    IIS上部署的ASP.NET站点都会在一个.Net Framework的特定目录下生成临时编译文件增加ASP.NET站点的访问性能,有时候需要手动去删除这些临时编译文件,特别是发布新版本代码到IIS后 ...

  2. 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube

    错误提示: 处理报表时出错. (rsProcessingAborted)对数据集“dsArea”执行查询失败. (rsErrorExecutingCommand)Team System 多维数据集或者 ...

  3. JavaScript进阶系列01,函数的声明,函数参数,函数闭包

    本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...

  4. Delphi 19种反调试检测法

    //使用IsDebuggerPresent这个API来检测是否被调试function FD_IsDebuggerPresent(): Boolean;beginif IsDebuggerPresent ...

  5. C#编程(四十二)----------委托和事件

    委托和事件 委托是C#总比较重要的概念,学习C#爱这里最容易产生迷惑. 有些时候,犹豫我们在开发程序时对后续可能出现的要求及变化考虑不足而导致麻烦,这些新变化可能导致程序的重新编写,那能不能改变这种情 ...

  6. Apache Tomcat 9 Installation on Linux (RHEL and clones)

    Apache Tomcat 9 is not available from the standard RHEL distributions, so this article provides info ...

  7. Ioc:autofac lifetime scope.

    During application execution, you’ll need to make use of the components you registered. You do this ...

  8. docker查看容器的网络ip

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id 可直 ...

  9. 西部世界第二季全集高清百度云在线观看BT种子迅雷下载

    原名:Westworld地区:美国语言:英语首播:2018-04-23 周一制作公司:HBO类型:科幻/惊悚/悬疑/西部别名:西方极乐园编剧:乔纳森·诺兰 / 丽莎·乔·诺兰导演:乔纳森·诺兰主演:埃 ...

  10. md5目录下的文件包括子目录

    find ./ -type f -print0 | xargs -0 md5sum