微软企业库5.0 学习之路——UnityPIAB 通过配置实现AOP
生活随笔
收集整理的這篇文章主要介紹了
微软企业库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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 针对不同浏览器的css
- 下一篇: 一天干掉一只Monkey计划(二)——热