接着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. [Winform]关于cefsharp触屏设备长按文本内容,崩溃问题的修复

    摘要 在之前遇到cefsharp,在触屏电脑上,长按文本内容,会崩溃的问题. 相关文章 当时遇到这样的问题,在cefsharp项目下提交了bug.已经修复,可以参考当时我提的bug,以及解决过程,可参 ...

  2. Windows Phone本地数据库(SQLCE):14、删除数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的最后一篇第十四篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需 ...

  3. js alert换行

    <script type="text/javascript"> alert("hello \n world!"); </script>

  4. 分布式系统:CAP

    一致不太理解CAP,最近好像有点感觉了,这里写下来,先介绍下CAP的定义: C:一致性.写完数据后,立马能看到最新数据. A:可用性.所有请求必须有响应. P:分区容错性.网络或服务器故障不会导致系统 ...

  5. Linux学习14-ab报错apr_pollset_poll: The timeout specified has expired (70007)

    前言 使用ab压力测试时候出现报错apr_pollset_poll: The timeout specified has expired (70007),本篇总结了几个ab常见的报错和对应解决办法 当 ...

  6. 解决VS2010连接VSS时,Access to file"\\***\rights.dat" denied

    1.通过VS2010打开项目链接VSS后,提示 Access to file"\\***\rights.dat" denied. 该提示是指没有网络访问的权限,用户要在共享文件夹有 ...

  7. Android 阴影,圆形的Button

    MainActivity.java package com.kale.gridlayout; import android.app.Activity; import android.graphics. ...

  8. Can't create pdf file with font calibri bold 错误解决方案

    错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...

  9. Guava ClassToInstanceMap

    概述 ClassToInstanceMap提供了一种是用Class作为Key, 对应实例作为Value的途径.他定义了T getInstance(Class<T>)和T putInstan ...

  10. 开放重定向(Open Redirection)

    简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击. 场景分析 假设有一个正规网站http://ne ...