6. “Hello World” Example with AspectDNG
In this chapter, let’s try to use AspectDNG to do an AOP example together.
Firstly, I define the base and aspect assemblies as below:
TestBaseClass.cs in BaseLib.dll
TestAspectClass.cs in AspectLib.dll
The AspectDngConfig and Advice configuration are as below:
AspectDngConfig.xml
HelloAspectDngAdvice.xml
After execute the command:
aspectdng.exe –w AspectDngConfig.xml
BaseLib.dll is statically weaved and the source code (decompiled by Salamander) becomes below:
Conclusion
The uncommented operations defined in HelloAspectDngAdvice.xml are executed successfully. The commented code was also tested, but either failed or ignored by AspectDNG, seams they haven’t been supported correctly or not supported yet. And we can also check the weaving log file.
BaseLib.weaving.xml
posted on 2005-09-20 13:15 Teddy's Knowledge Base 阅读(2283) 评论(8) 编辑 收藏 网摘 所属分类: AOSD
为什么不同时demo一下在原有类中织入新的方法, 这样全面一点. 回复 引用 查看
织入新方法和新的类是一样的,只要这样定义Advice就行: <Insert aspectXPath="//Type[@name = 'MethodToBeInserted']" targetXPath="//ClassToAddMethod"/> 因为原来想测试insert一个类,然后再将这个新的类设为原有的一个类的基类的,但是SetBaseType这样Operation似乎支持的还不好,总是设不对,所以最后把SetBaseType给comment了,只保留了这个Instert。 回复 引用 查看
So complicated. A little scared. kaka~~~ 回复 引用 查看
修正一些错误,请修改advice中的如下语句: <MakeSerializable targetXPath="//Type[@name = 'TestBaseClass']"/> 原来不小心将targetXPath的类型设为Method,所以出错,改成Type就好了! 而下面的Warning和Error也可以去掉注释,原来这两项只是用于aspectdng.exe本身的,并不会真的向原代码织入任何代码,我理解错误了!当然加入error的话aspectdng会执行失败。 <!--<Warning targetXPath="//Method[@name = 'MethodWarningToBeAdded']"/>--> <!--<Error targetXPath="//Method[@name = 'MethodErrorToBeAdded']"/>--> 回复 引用 查看
为了避免AspectDNG现有Configuration的限制和使用上的不方便,我将再下一章中构建一个基于AspectDNG's ILML Library的全新的Apsect Weaver的雏形,之后的AOP Practice章节也将使用这个新的AOP Framework - Teddy's Aspect Weaver,敬请期待! 回复 引用 查看
我把文件挡下来了,可是不好用呀,哪位大虾指点一下.我是在C#环境下实行的。 回复 引用
TEDDY,你好!你水平很好,可是文中还少点讲解心得之类的。这只是我个人的一个小看法(当然主要原因还是因为我刚接触框架之类的,所以一下没看懂,我还会继续看的,有一个Kanas的框架,不知你看过没有),谢谢你的精采献出! 回复 引用
这种aop的确和应用context的非常的不同,不好理解. 回复 引用 查看
Powered by: 博客园 Copyright © Teddy's Knowledge Base