component service - AOP
http://ms.mblogger.cn/yysun/posts/2629.aspx
什么是 component service?就是一个对象/component的运行环境,你把你的对象/component放到这个环境中,定义一下这个对象的方法需要事务支持、需要用户验证支持 ... 那么你的对象就能获得这种支持,而对象本身的逻辑处理不需要任何修改。
有这等好事?可不是吗。实际上 COM+ enterprise service 和 J2EE EJB container 就是所谓的 component service.
问题出在 COM+ 也好,J2EE 也好用起来真是太麻烦。
因此,我不得不动手自己建了一个微型的 component service,来支持事务(Transaction)处理。
http://www.delphibbs.com/keylife/iblog_show.asp?xid=3752
为了做这个,动用了一个 .NET 未公布的秘密武器 Context - Context Attribute。
如果去 MSDN 检索 Context 或者 ContextAttribute,您会看到:
This type supports the .NET Framework infrastructure and is not intended to be used directly from your code.
但是,我仍然找到了两篇文章:
1、Decouple Components by Injecting Custom Services into Your Object's Interception Chain
http://msdn.microsoft.com/msdnmag/issues/03/03/contextsinnet
这个 Injecting 使我写了 Injection 这篇笔记。
http://www.delphibbs.com/keylife/iblog_show.asp?xid=3765
2、AOP: Aspect-Oriented Programming Enables Better Code Encapsulation and Reuse
http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/default.aspx
到这里,我突然发现我已经进入了 AOP 的世界。因为这篇文章介绍的方法和我用的方法一样。
回过头想想,就是 AOP。 ?
所谓的 AOP 就是把处理逻辑分开出来,例如,下面一个程序过程:
fun() {
Authentication();
Log();
BeginTransaction();
try {
doSomething();
Log();
Commit();
}
catch {
Rollback();
log();
}
}
这是很典型、很传统的程序逻辑。但是,按照 AOP,则它可以分成4个 Aspect (方面):
Aspect 1: log
Aspect 2: Transaction
Aspect 3: Authentication
Aspect 4: doSomething,这是主逻辑。
因此, AOP 的程序应该是
/**this function needs: log, Transaction, Authentication**/
fun() {
doSomething();
}
也就是说,通用的逻辑,例如 log, Transaction, Authentication ... 可以标定到主逻辑上去,而不用写到主逻辑里面。而运行系统在运行主逻辑的时候,把这些功能加上。
把各种功能合成起来,AOP 的术语成为 weaving - 编织,很形象啊。
那么我的那个支持 declarative transaction 的 mini component service 正是符合 AOP 的思想,因为,我可以用 C# 属性来定义 fun() 由 transaction 保护起来。AutoComplete 意思是自动 commit/rollback。
[AutoComplete]
fun() {
}
同理,还可以
[AutoComplete, Log, Trace, Authentication('sysadmin')]
fun() {
}
完全就是 AOP 了!真是无意中踏进了 AOP,现在再看 AOP 的资料,一目了然了。
http://en.wikipedia.org/wiki/Aspect-oriented_programming

浙公网安备 33010602011771号