.Net里的Attribute 学习
.Net里的Attribute 学习
前两天看到书里边讲Attribute定制,结合了网上的资料,自己做了简单的登录功能,并结合了某些设计模式,有兴趣的朋友可以看下。由于时间原因,没有做过多的说明,直接上代码,希望能帮助哪些不会的初学者,同时也希望得到高人的指点,本人将虚心接受批评,谢谢!
用户登录操作类:
using System;
using LemonFreamworkAOP.User;
using LemonFreamworkAOP.AOP; namespace LemonFreamworkAOP.User
{
[LoginAOP]
public class UserDAL : ContextBoundObject, IUser
{
private UserInfo userInfo; public UserDAL(UserInfo userInfo)
{
this.userInfo = userInfo;
}
public Boolean IsLogin(UserInfo user)
{
return this.userInfo == user;
}
}
}
下边是几个核心抽象基类类:
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPProperty : IContextProperty, IContributeObjectSink
{
protected virtual String GetName
{
get { return "AOP"; }
} protected virtual Boolean IsOK(Context newCtx)
{
return true;
} protected abstract IMessageSink GetSink(IMessageSink nextSink); #region IContextProperty IContributeObjectSink
public void Freeze(Context newContext)
{ } public Boolean IsNewContextOK(Context newCtx)
{
return IsOK(newCtx);
} public String Name
{
get { return GetName; }
} public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return GetSink(nextSink);
}
#endregion
}
}
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPAttribute : ContextAttribute
{
public AOPAttribute() : base("AOP") { } public sealed override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
ctorMsg.ContextProperties.Add(CreateAOPProperty());
} protected abstract AOPProperty CreateAOPProperty();
}
}
using System;
using System.Collections.Generic;
using LemonFreamworkAOP.User;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public abstract class AOPSink : IMessageSink
{
private Dictionary<String, Object> beforeHandles;
private Dictionary<String, Object> afterHandler;
private IMessageSink messageSink;
protected const String errorDesc = "Not Find MethodName !"; public AOPSink(IMessageSink messageSink)
{
this.beforeHandles = new Dictionary<String, Object>();
this.afterHandler = new Dictionary<String, Object>();
this.messageSink = messageSink;
AddAllBeforeAOPHandle();
AddAllAfterAOPHandle();
} public virtual void AddBeforeAOPHandle(String methodName, BeforeAOPHandle beforeHandle)
{
if (String.IsNullOrEmpty(methodName)) return;
if (!beforeHandles.ContainsKey(methodName))
beforeHandles.Add(methodName, beforeHandle);
} public virtual void AddAfterAOPHandle(String methodName, AfterAOPHandle aftereHandle)
{
if (String.IsNullOrEmpty(methodName)) return;
if (!afterHandler.ContainsKey(methodName))
afterHandler.Add(methodName, aftereHandle);
} public virtual BeforeAOPHandle FindBeforeAOPHandle(String methodName)
{
if (beforeHandles.ContainsKey(methodName))
return beforeHandles[methodName] as BeforeAOPHandle;
else
throw new Exception(errorDesc);
} public virtual AfterAOPHandle FindAfterAOPHandle(String methodName)
{
if (afterHandler.ContainsKey(methodName))
return beforeHandles[methodName] as AfterAOPHandle;
else
throw new Exception(errorDesc);
} public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
} public IMessageSink NextSink
{
get { return this.messageSink; }
} public IMessage SyncProcessMessage(IMessage msg)
{
IMethodCallMessage callMsg = msg as IMethodCallMessage;
BeforeProcess(callMsg); IMessage imsg = messageSink.SyncProcessMessage(msg); //IMethodReturnMessage retMsg = imsg as IMethodReturnMessage;
//AfterProcess(callMsg.MethodName, retMsg); return imsg;
} protected abstract void AddAllBeforeAOPHandle();
protected abstract void AddAllAfterAOPHandle(); protected virtual void BeforeProcess(IMethodCallMessage callMsg)
{
String methodName = callMsg.MethodName;
BeforeAOPHandle beforeHandle = FindBeforeAOPHandle(methodName);
if (beforeHandle != null)
beforeHandle(callMsg);
} protected virtual void AfterProcess(String methodName, IMethodReturnMessage retMsg)
{
AfterAOPHandle afterHandle = FindAfterAOPHandle(methodName);
if (afterHandle != null)
afterHandle(retMsg);
}
}
}
以下是登录功能的派生子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace LemonFreamworkAOP.AOP
{
[AttributeUsage(AttributeTargets.Class)]
public class LoginAOPAttribute : AOPAttribute
{
protected override AOPProperty CreateAOPProperty()
{
return new LoginAOPProperty();
}
}
}
using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public class LoginAOPProperty : AOPProperty
{
protected override IMessageSink GetSink(IMessageSink nextSink)
{
return new LoginAOPSink(nextSink);
}
}
}
using System;
using LemonFreamworkAOP.User;
using LemonFreamworkAOP.Untity;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging; namespace LemonFreamworkAOP.AOP
{
public class LoginAOPSink : AOPSink
{
public LoginAOPSink(IMessageSink messageSink) : base(messageSink) { } public void BeforeUserLogin(IMethodCallMessage callMsg)
{
UserInfo loginUser = callMsg.GetArg(0) as UserInfo;
UserInfo configUser = ConfigHelper.GetUser;
if (loginUser.UserName != configUser.UserName || loginUser.Password != configUser.Password)
throw new Exception("用户名或密码不正确!");
} protected override void AddAllBeforeAOPHandle()
{
AddBeforeAOPHandle("IsLogin", BeforeUserLogin);
} protected override void AddAllAfterAOPHandle()
{ }
}
}
.Net里的Attribute 学习的更多相关文章
- 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版
代码走查25条疑问 代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...
- C# Attribute学习
由于项目中需要使用到序列化相关的技术,从而想到是否可以使用C#中的特性,特此花了近两小时学习了一下. 对于特性的学习,主要参考了两篇博文,特此感谢.以下附链接: http://www.cnblogs. ...
- .Net 特性 attribute 学习 ----自定义特性
什么是特性? [Obsolete("不要用无参构造函数",true)] 放在方式上, 该方法就不能使用了 [Serializable]放在类上面.该类就是可以序列化和反序列化使用 ...
- Qt 的线程与事件循环——可打印threadid进行观察槽函数到底是在哪个线程里执行,学习moveToThread的使用)
周末天冷,索性把电脑抱到床上上网,这几天看了 dbzhang800 博客关于 Qt 事件循环的几篇 Blog,发现自己对 Qt 的事件循环有不少误解.从来只看到现象,这次借 dbzhang800 的博 ...
- C#特性Attribute学习
起初一直纠结于如何调用特性附着在下面那个成员的值,后来发现不需要调用,通过反射加载的时候是自动绑定上去的,即 获得成员对象之后,有一个方法可以获得特性标签. 其实从类库提供者,和类库使用者的角度,分开 ...
- laravel里的队列学习
首先,我们要搞明白几个概念,从小到大依次有:队列任务,队列,连接. 他们属于依次被包含的关系,一个队列里有许多的队列任务,一个连接中可以有许多队列. 队列任务:对每个用户都会进行的操作,理解为队列任务 ...
- 用python制作文件搜索工具,深挖电脑里的【学习大全】
咳咳~懂得都懂啊 点击此处找管理员小姐姐领取正经资料~ 开发环境 解释器: Python 3.8.8 | Anaconda, Inc. 编辑器: pycharm 专业版 先演示效果 开始代码,先导入模 ...
- odoo里的javascript学习---自定义插件
插件效果图 定义js odoo.define('auto_widget',function(require){ "use strict"//通过扩展AbstractField来扩展 ...
- [0406]学习一个——Unit 1 Html、CSS与版本控制
前言 最近发现了Github的Student认证,本来想用来注册Digital Ocean搭个梯子,结果注册验证不能用VISA借记卡=~=. 那么在这漫长的清明节假期里,只有学习能满足空虚的内心(划掉 ...
随机推荐
- 将字符串“abc”全排列成:abc、acb、bac、bca、cab、cba
[STAThread] static void Main() { string s = "abcd"; ...
- JQ优化性能
一.注意定义jQuery变量的时候添加var关键字这个不仅仅是jQuery,所有javascript开发过程中,都需要注意,请一定不要定义成如下:$loading = $('#loading'); / ...
- 【 D3.js 入门系列 --- 9.6 】 生产的包图
我的个人博客是:www.ourd3js.com csdn博客为:blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 打包图( Pack ).用于包括与被包括的关系,也表示各个对象的权重, ...
- FlexiGrid使用手册
FlexiGrid使用手册 一.概览 Flexigrid是一个基于jQuery开发的Grid,与 Ext Gird类似.Flexigrid显示的数据能够通过Ajax获取或者从一个普通的表格转换. 它的 ...
- SQL Server之记录筛选(top、ties、offset)汇总
一.TOP 筛选 如果有 ORDER BY 子句,TOP 筛选将根据排序的结果返回指定的行数.如果没有 ORDER BY 子句,TOP 筛选将按照行的物理顺序返回指定的行数. 1. 返回指定数目的行 ...
- 用jQuery的ajax的功能实现输入自动提示的功能
注意事项:要使用jQuery首先要把它的包引用进来( <script type="text/javascript" language="javascript&quo ...
- Inno Setup技巧[界面]欢迎页面上添加文字
原文:Inno Setup技巧[界面]欢迎页面上添加文字 本文介绍在"欢迎页面添加文字"的两种方法. 界面预览: Setup技巧[界面]欢迎页面上添加文字" title= ...
- 为mongodb加上权限
我们知道mysql在安装的时候需要我们设置一个数据库默认的用户名和密码,mongodb也不例外,不过mongodb是默认的没有设置访问限制的,不需要输入用户名和密码都可以访问的,但是这样会十分的不安全 ...
- Hudson+Maven+Svn搭建持续集成环境
Hudson+Maven+Svn搭建持续集成环境 博客分类: 配置管理 mavenSVNTomcat项目管理配置管理 一.所用开发工具 1. Hudson: Hudson 是一种革命性的开放源码 ...
- INSTEAD OF触发器
Oracle触发器5(INSTEAD OF触发器) 前提:对于简单的视图,可以直接进行DML操作,但是对于复杂视图,不允许直接执行DML操作,当视图符合以下任何一种情况都不可以: 具有集合操作符(UN ...