假设有一个系统,需要权限控制,按 oo 的方法,做个 SecurityManager, just like this:
public class SecurityManager{
public bool checkPower(User user, AcessModel am){
///here to judge user if had access to system
return true;
}
}
然后在业务逻辑中进行如下调用:
public class BusinessClass{
public void businessMethod(){
SecurityManager sm= new SecurityManager();
if(sm.checkPower(user,am)){
///you had past the check
….
}
}
}
这就造成系统的权限校验与业务逻辑掺杂一起,耦合增加,使程序的鲁棒性大打折扣。。。
但如果我们用aop则会将其彻底改观,对于websharp,可以做如下更改:
public class BusinessClass :AspectObject{
/* 继承AspectObject,去掉businessMethod()中的:
SecurityManager sm= new SecurityManager();
if(sm.checkPower(user,am)){
///you had past the check
….
}
*/
}
public class SecurityManager :IAspect{
///implement IAspect interface by execute method as followed:
public void execute(Object[] paramList){
///here to judge user if had access to system
}
}
最后在配置文件中添加:
<Websharp-Aspects>
<Aspect type=”pack.SecurityManager,pack” deploy-model=”Singleton” pointcut-type=”Method” action-position=”before” match=”*,*”/>
</Websharp-Aspects >
以上你就已实现一个aop! 这样Security 和Business就独立了。。。感觉websharp里对AOP的概念可能局限