完全通过配置实现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相关推荐

  1. [EntLib]微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——上篇...

    本文是为后面的学习之路做铺垫,简单介绍下企业库中的Validation模块的一些相关知识,包括Validation模块的简介.用途.使用方法.默认提供的多种验证器的介绍等. 一.简介及用途 在实际的项 ...

  2. [EntLib]微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—上篇...

    在完成了后,今天开始介绍企业库中的新模块:Cryptographer(加密模块),这个模块在日常的大多数项目的作用非常重要,例如:网站会员密码.身份证号.网站配置等,通过对信息进行加密可以保证项目数据 ...

  3. (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)...

    原文地址:http://www.cnblogs.com/kyo-yo/archive/2010/11/01/Learning-EntLib-Tenth-Decoupling-Your-System-U ...

  4. [EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART5——使用Unity自身的拦截器...

    在前一篇文章中,介绍了如何使用Unity来接管PIAB进行拦截操作,而Unity自身也提供了类似于ICallHandler的拦截处理程序--IInterceptionBehavior,今天这篇文章就是 ...

  5. 微软企业库5.0学习笔记(四)配置企业库

    http://blog.csdn.net/sfbirp/archive/2010/05/18/5603567.aspx 转载于:https://www.cnblogs.com/Rising/archi ...

  6. 微软企业库5.0学习笔记(三十三)数据访问模块

    前言 鉴于企业库5.0已经发布正式版,同时有广大读者的要求(臭屁一下,o(∩_∩)o...),后面文章的内容和代码将基于Enterprise Library5.0和Unity2.0来写,感谢大家的一贯 ...

  7. 微软企业库4.1学习笔记(三)企业库迁移和并行使用,以及企业库的扩展

    一.迁移和并行使用 通常来说,企业库是建立在.NET 2.0 的基础上,使用后续版本的企业库也不需要改变任何代码.不需要将引用更新到新的程序集,也不需要在配置文件中指明程序集的正确版本. 这个版本4. ...

  8. 微软企业库4.1学习笔记(八)创建对象 续集2

    3.3通过配置指定和Unity的整合 另外一种方法是在配置源中指定配置的需要,你可以指定下面的一条或者多条: 你可以在Unity配置中指定想要的BlockExtensions 你可以在Unity配置中 ...

  9. 微软企业库4.1学习笔记(十)企业库的设计

    在设计整个企业库的过程中,使用了一系列的最佳实践.下面列出一些里面的最佳实践: 在核心库中使用了Common模块 使用了统一的命名约定和统一的版本 在设计的过程中使用单元测试 在所有模块中包含基础结构 ...

最新文章

  1. Bioinformatics: Assembling Genomes (week 1-2)
  2. UFLDL:稀疏自编码器
  3. OpenJDK将对Android开发产生怎样的影响?
  4. 冒泡排序和鸡尾酒排序(改进的冒泡排序)
  5. 2019年python就业前景_2019年Python数据挖掘就业前景前瞻
  6. python中的正则表达式是干嘛的_操作python中的正则表达式(上)
  7. 中反应器体积_缠绕管式反应器大幅提高能效,移热能力较列管式反应器提升逾50%...
  8. 操作系统之内存管理:4、基本地址变换机构(段氏、页式、段页式)
  9. css的几种垂直水平居中方法
  10. 最短路算法(3种算法)
  11. matlab2c使用c++实现matlab函数系列教程-rand函数
  12. 【个人笔记】OpenCV4 C++ 快速入门 22课
  13. AngularJS中的DOM value与view value
  14. 不能查看工作组计算机 提示没有权限拒绝访问
  15. 吴恩达深度学习第一章第二周编程作业
  16. 大地坐标高斯/UTM投影计算工具
  17. Hadoop_MapperContextInputSplitFileSplit源码浅析
  18. java实现将word转换pdf
  19. draft伦理——第七章
  20. sort-选择排序法

热门文章

  1. CF-241 E.Flights(差分约束)
  2. HDU Problem - 5918 Sequence I
  3. const_iterator简单介绍
  4. 计组-数据通路的功能和基本结构
  5. 排序算法——快速排序算法
  6. mysql 主备及时_MySQL高可用(二)主备延时如何解决?
  7. RPC框架系列——Protocol Buffers
  8. yii框架下使用redis
  9. Android(五)——dex文件动态调试
  10. 【原创】分布式之大话CAP