通过fsharp 使用Enterprise Library Unity 2

接着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多次。记录的条目也会对应增多。应该能够想象结构上的变化。

以上。

时间: 2024-08-29 05:17:17

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

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

这篇就三种拦截模式进行一下探索. 特性总结   类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentProxyInterceptor           Instance 多接口(接口之间能够切换)  MarshalByRef 执行缓慢 接口类型(virtual, non-virtual, or interface) 类内部函数互相引用能够引起拦截行为 VirtualMethodInterceptor

通过fsharp 使用Enterprise Library Unity

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用Ioc概念的Unity库的优点,简单的说就是进一步解耦系统各组件的依赖关系.客户端代码只需依赖需要使用的接口(服务)就可以快速的进行开发.</span> 1.基本流程,定义类-->配置container-->解析类,并使用-->析构(依策略) 2.注册的

通过fsharp 使用Enterprise Library Unity 4 - Policy Interjection

policy interception Interception class拦截器还可以用Policy 以一种更高效的方式进行加载使用.拦截和被拦截对象的关系常见的是一对多,因为被拦截的主要是对象的方法所以有时数量巨大.Policy具有定义匹配模式的功能,所以可以简化这一匹配工作,不用一一添加被拦截对象.匹配的模式有 ? Assembly name ? Namespace ? Type ? Tag attribute ? Custom attribute ? Member name ? Meth

通过fsharp探索Enterprise Library DataBase 1.2

上一次讲到Enterprise Library中Data Access 模块的配置以及简单SQL语句和存储过程的执行.在探索的过程中应用Fsharp语言和交互环境能够马上看到结果,这感觉真的是非常通透. 1.提高数据库操作的复杂性,加入参数的变化,这一点和ADO的操作没有太多的不同. SQL语句带参数 let sqlStatement = "select top 1 * from OrderList where State like @state" using(defaultDB.Ge

通过fsharp探索Enterprise Library 6 DataBase 1.3 Sqlite

使用Enterprise Library就是为了尽可能少的开发常用组件.数据库在选择的过程中常会面临部署,版权,个人喜好等诸多考量.最佳的处理方法就是添加一层数据抽象层,再切换Enterprise的过程中进行无缝衔接.由于我的笔记本跑的越来越慢,又想尝试一下使用Sqlite所以,就用Sqlite做例子. 我用的是Enterprise Library 6.0原本以为只需要简单的配置就可以进行实验,没想到GitHub上Sqlite提供的Sqlite兼容组件到Enterprise Library 4.

通过Fsharp探索Enterprise Library Exception

Exception怎么生成是一回事,怎么展示又是还有一回事了. Exception Block主要关注的点在于Exception信息的展示.Exception不同于一般的log信息,是系统设计者未考虑的错误情况.当异常出现时,错误的情况,或者暴露一些比較敏感的系统信息.或者将一些不怎么友好的信息显示给一些不怎么友好的客户.这时一个计算机异常就引入了一个客户异常,一个终极异常.所以异常处理的目标就是截断异常,进而恢复系统. 把合理的异常信息显示给相相应的用户. 因此主要的异常处理脉络也出现了.1.

Enterprise Library 5.0 系列教程

1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) 3. Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 4. Microsoft Enterpr

Microsoft Enterprise Library 简介与请大家下载Microsoft Enterprise Li

什么是Enterprise Library     Enterprise Library是一组应用程序块(Application Block)的集合.他们是可重用的软件组件,被设计用来帮助开发者面对常用的企业级开发任务.用来解决我们在企业级开发中遇到常见问题,如配置管理.数据访问.缓存管理.记录操作日志.异常管理.加密解密.权限管理等.它是对很多有影响力的软件企业通过多年的开发实践积累下来的技术的整合当.前的版本是2006年1月发布的,基于.NET framework 2.0.这个版本包含了大量

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

在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreSQL.IBM DB2.或者国产达梦数据库等等,这些数据库的共同特点是关系型数据库,基本上开发的模型都差不多,不过如果我们基于ADO.NET的基础上进行开发的话,那么各种数据库都有自己不同的数据库操作对象,微软企业库Enterprise Library是基于这些不同数据库的操作做的抽象模型,适合多数据