微软企业库5.0 学习之路——UnityPIAB 通过配置实现AOP
完全通过配置实现AOP的资料太少了,今天忙了好几个小时终于走通了,分享一下。
模拟业务是:登陆,记录登陆日志。
// 业务接口
public interface IStudentManage{bool Login(string userId, string userName);}public class StudentManage:IStudentManage{public StudentManage() { }public bool Login(string userId, string userName){return userId == "123" && userName == "123";}}/// <summary>/// 登陆日志/// </summary>public class UserLogCallHandler : ICallHandler{/// <summary>/// 构造函数,此构造函数是用于Attribute调用 /// </summary>/// <param name="message"> 消息 </param>/// <param name="parameterName"> 参数名 </param>public UserLogCallHandler(string message, string parameterName){this.Message = message;this.ParameterName = parameterName;}public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext){//检查参数是否存在if (input == null) throw new ArgumentNullException("input");if (getNext == null) throw new ArgumentNullException("getNext");//开始拦截,此处可以根据需求编写具体业务逻辑代码//调用具体方法var result = getNext()(input, getNext);// 记录登陆日志}public string Message { get; set; }public string ParameterName { get; set; }private int order = 0;public int Order{get{return order;}set{order = value;}}}
最关键的XML配置
<configuration><configSections><section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/></configSections><unity xmlns="http://schemas.microsoft.com/practices/2010/unity"><assembly name="UnityAOPConfig"/><namespace name="UnityAOPConfig"/><!--新增配置节扩展,用于下面的<interception>配置节--><sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" /><container name="LoginContainer"><!--为容器增加Interception扩展,如不增加下面interception配置会报错--><extension type="Interception"/><interception><!--增加一个名为UserLog的拦截策略,用于记录日志--><policy name="UserLog"><!--新增MemberNameMatchingRule匹配规则,同时需要同过配置初始化匹配规则构造函数具体可查看Unity.InterceptionExtension.PolicyInjection.MatchRule下具体类--><matchingRule name="rule1" type="MemberNameMatchingRule"><lifetime type="singleton"/><constructor><param name="nameToMatch" value="Login"/></constructor></matchingRule><!--增加调用处理程序,这边指定的是我自定义的UserLogCallHandler--><!--同样也需要初始化构造函数--><callHandler name="handler1"type="UserLogCallHandler"><lifetime type="singleton"/><constructor><param name="message" type="System.String" value="登录成功!"></param><param name="parameterName" type="System.String" value=" "></param></constructor></callHandler></policy></interception><!--注册对象关系,需要注意的是需要为这个注册增加TransparentProxyInterceptor的拦截器--><register type="IStudentManage" mapTo="StudentManage"><interceptor type="TransparentProxyInterceptor" /><interceptionBehavior type="PolicyInjectionBehavior" /></register></container></unity>
</configuration>
前台调用代码
public static IUnityContainer GetIUnityContainer(string nodeName){//根据文件名获取指定config文件var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "Web.config" };//从config文件中读取配置信息Configuration configuration =ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");var container = new UnityContainer().LoadConfiguration(unitySection, nodeName);return container;}protected void btnSubmit_Click(object sender, EventArgs e){IUnityContainer container = new UnityContainer().LoadConfiguration("LoginContainer");IStudentManage studentBll = container.Resolve<IStudentManage>();if (studentBll.Login(txtUid.Text.Trim(), txtPwd.Text.Trim())){if (string.IsNullOrEmpty(Request.QueryString["returnUrl"]) == false){Response.Redirect(Request.QueryString["returnUrl"]);}else{Response.Redirect("~/Default.aspx");}}else{ltMsg.Text = "用户名或密码不正确,请重试!";}}}
转载于:https://www.cnblogs.com/dyfzwj/archive/2011/11/21/2257279.html
微软企业库5.0 学习之路——UnityPIAB 通过配置实现AOP相关推荐
- [EntLib]微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——上篇...
本文是为后面的学习之路做铺垫,简单介绍下企业库中的Validation模块的一些相关知识,包括Validation模块的简介.用途.使用方法.默认提供的多种验证器的介绍等. 一.简介及用途 在实际的项 ...
- [EntLib]微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—上篇...
在完成了后,今天开始介绍企业库中的新模块:Cryptographer(加密模块),这个模块在日常的大多数项目的作用非常重要,例如:网站会员密码.身份证号.网站配置等,通过对信息进行加密可以保证项目数据 ...
- (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)...
原文地址:http://www.cnblogs.com/kyo-yo/archive/2010/11/01/Learning-EntLib-Tenth-Decoupling-Your-System-U ...
- [EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART5——使用Unity自身的拦截器...
在前一篇文章中,介绍了如何使用Unity来接管PIAB进行拦截操作,而Unity自身也提供了类似于ICallHandler的拦截处理程序--IInterceptionBehavior,今天这篇文章就是 ...
- 微软企业库5.0学习笔记(四)配置企业库
http://blog.csdn.net/sfbirp/archive/2010/05/18/5603567.aspx 转载于:https://www.cnblogs.com/Rising/archi ...
- 微软企业库5.0学习笔记(三十三)数据访问模块
前言 鉴于企业库5.0已经发布正式版,同时有广大读者的要求(臭屁一下,o(∩_∩)o...),后面文章的内容和代码将基于Enterprise Library5.0和Unity2.0来写,感谢大家的一贯 ...
- 微软企业库4.1学习笔记(三)企业库迁移和并行使用,以及企业库的扩展
一.迁移和并行使用 通常来说,企业库是建立在.NET 2.0 的基础上,使用后续版本的企业库也不需要改变任何代码.不需要将引用更新到新的程序集,也不需要在配置文件中指明程序集的正确版本. 这个版本4. ...
- 微软企业库4.1学习笔记(八)创建对象 续集2
3.3通过配置指定和Unity的整合 另外一种方法是在配置源中指定配置的需要,你可以指定下面的一条或者多条: 你可以在Unity配置中指定想要的BlockExtensions 你可以在Unity配置中 ...
- 微软企业库4.1学习笔记(十)企业库的设计
在设计整个企业库的过程中,使用了一系列的最佳实践.下面列出一些里面的最佳实践: 在核心库中使用了Common模块 使用了统一的命名约定和统一的版本 在设计的过程中使用单元测试 在所有模块中包含基础结构 ...
最新文章
- Bioinformatics: Assembling Genomes (week 1-2)
- UFLDL:稀疏自编码器
- OpenJDK将对Android开发产生怎样的影响?
- 冒泡排序和鸡尾酒排序(改进的冒泡排序)
- 2019年python就业前景_2019年Python数据挖掘就业前景前瞻
- python中的正则表达式是干嘛的_操作python中的正则表达式(上)
- 中反应器体积_缠绕管式反应器大幅提高能效,移热能力较列管式反应器提升逾50%...
- 操作系统之内存管理:4、基本地址变换机构(段氏、页式、段页式)
- css的几种垂直水平居中方法
- 最短路算法(3种算法)
- matlab2c使用c++实现matlab函数系列教程-rand函数
- 【个人笔记】OpenCV4 C++ 快速入门 22课
- AngularJS中的DOM value与view value
- 不能查看工作组计算机 提示没有权限拒绝访问
- 吴恩达深度学习第一章第二周编程作业
- 大地坐标高斯/UTM投影计算工具
- Hadoop_MapperContextInputSplitFileSplit源码浅析
- java实现将word转换pdf
- draft伦理——第七章
- sort-选择排序法