接着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. Versaloon -- Connect To Targets

    Versaloon Full open-source(GPLv3) platform for multiple applications, including programmer, debugger ...

  2. STM32F4 Alternate function mapping

    #define GPIO_AF0_MCO // MCO (MCO1 and MCO2) Alternate Function mapping #define GPIO_AF0_RTC_50Hz // ...

  3. 智能指针scoped_ptr

    对应C++11中的unique_ptr #include <iostream> #include <memory> class foo { public: foo() { st ...

  4. self.xxx = nil 可以等效于[_xxx release] _xxx= nil 么

    如果属性是copy.retain的话是等价的.如下: - (void)setXXX:(NSString*)axx { if (_xxx != axx) { [_xxx release]; _xxx = ...

  5. windows下PHP不能开启pgsql扩展的解决方法

    Tip: 环境 windows8.1 64位 + xampp1.8.1 + postgresql 9.3.6-2 第一步: php.ini中开启pgsql扩展  extension=php_pgsql ...

  6. .NET:Assembly.CodeBase vs. Assembly.Location

    The CodeBase is a URL to the place where the file was found, while the Location is the path from whe ...

  7. struct net_device网络设备结构体详解

    转自:http://blog.csdn.net/viewsky11/article/details/53046787 在linux中使用struct net_device结构体来描述每一个网络设备.同 ...

  8. 罪恶黑名单第四季/全集The Blacklist迅雷下载

    英文全名The Blacklist,第1季(2016)NBC.本季看点:<罪恶黑名单>我们知道:剧情紧接第三季结尾,每个人——Liz,Red以及特别行动组的其他人——似乎都有许多故事可说: ...

  9. mysql update select

    根据文件名 更新外键ID UPDATE tb_obj  INNER JOIN tb_img ON tb_img.filename=tb_obj.filename  SET tb_objinfo.img ...

  10. Material Designer的低版本兼容实现(四)—— ToolBar

       Toolbar其实是一个ActionBar的变体,大大扩展了Actionbar.我们可以像对待一个独立控件一样去使用ToolBar,可以将它放到屏幕的任何位置,不必拘泥于顶部,还可以将它改变高度 ...