Teddy's Knowledge Base

“Hello World” Example with AspectDNG


Return to Contents


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

using System;

namespace BaseLib
{
    
/// <summary>
    
/// Summary description for TestBaseClass.
    
/// </summary>

    public class TestBaseClass
    
{
        
public TestBaseClass()
        
{
        }


        
public void MethodToBeDeleted()
        
{
        }


        
public void MethodWarningToBeAdded()
        
{
        }


        
public void MethodErrorToBeAdded()
        
{
        }


        
public void MethodToTestInlineConstructorCall()
        
{
            
object obj = new TestBaseClass();
        }


        
public void MethodToTestInlineMethod()
        
{
        }


        
public void MethodToTestInlineMethodCall()
        
{
            MethodToTestInlineMethod();
        }


        
private string fieldToTestInlineField = "fieldToTestInlineField";

        
public void MethodToTestInlineFieldAccess()
        
{
            
string str = fieldToTestInlineField;
            fieldToTestInlineField 
= "WriteFieldToTestInlineField";
        }


        
public void MethodToTestAroundBody()
        
{
            Console.Write(
"MethodToTestAroundBody");
        }


        
public void MethodToTestAroundCall()
        
{
            MethodToTestAroundBody();
        }

    }

}

TestAspectClass.cs in AspectLib.dll

using System;

namespace AspectLib
{
    
/// <summary>
    
/// Summary description for TestAspectClass.
    
/// </summary>

    public class TestAspectClass
    
{
        
public TestAspectClass()
        
{
        }


        
public void CodeToBeAddedAsInlineBeforeConstuctorCall()
        
{
            Console.Write(
"CodeToBeAddedAsInlineBeforeConstuctorCall");
        }


        
public void CodeToBeAddedAsInlineAfterConstuctorCall()
        
{
            Console.Write(
"CodeToBeAddedAsInlineAfterConstuctorCall");
        }


        
public void CodeToBeAddedAsInlineBeforeMethodCall()
        
{
            Console.Write(
"CodeToBeAddedAsInlineBeforeMethodCall");
        }


        
public void CodeToBeAddedAsInlineAfterMethodCall()
        
{
            Console.Write(
"CodeToBeAddedAsInlineAfterMethodCall");
        }


        
public void CodeToBeAddedAsInlineAtStart()
        
{
            Console.Write(
"CodeToBeAddedAsInlineAtStart");
        }


        
public void CodeToBeAddedAsInlineBeforeReturn()
        
{
            Console.Write(
"CodeToBeAddedAsInlineBeforeReturn");
        }


        
public void CodeToBeAddedAsInlineBeforeFieldReadAccess()
        
{
            Console.Write(
"CodeToBeAddedAsInlineBeforeFieldReadAccess");
        }


        
public void CodeToBeAddedAsInlineBeforeFieldWriteAccess()
        
{
            Console.Write(
"CodeToBeAddedAsInlineBeforeFieldWriteAccess");
        }


        
public void CodeToBeAddedAsInlineAfterFieldReadAccess()
        
{
            Console.Write(
"CodeToBeAddedAsInlineAfterFieldReadAccess");
        }


        
public void CodeToBeAddedAsInlineAfterFieldWriteAccess()
        
{
            Console.Write(
"CodeToBeAddedAsInlineAfterFieldWriteAccess");
        }


        
public void CodeToBeAddedAsAroundBody()
        
{
            Console.Write(
"CodeToBeAddedAsAroundBody");
        }


        
public void CodeToBeAddedAsAroundCall()
        
{
            Console.Write(
"CodeToBeAddedAsAroundCall");
        }


    }


    
public class TestClassToBeInserted
    
{
    }

}

The AspectDngConfig and Advice configuration are as below:

AspectDngConfig.xml

<?xml version="1.0" encoding="utf-8"?>
<AspectDngConfig xmlns="http://www.dotnetguru.org/AspectDNG"
    debug
="false" 
    logWarnings
="true" logWarningsPath="Warnings.log"
    logIlml
="true" logIlmlPath="BaseLib.ilml.xml"
    logWeaving
="true" logWeavingPath="BaseLib.weaving.xml"
    validateRules
="true">
    
    
<BaseAssembly>BaseLib/bin/Debug/BaseLib.dll</BaseAssembly>
    
<AspectsAssembly>AspectLib/bin/Debug/AspectLib.dll</AspectsAssembly>
    
    
<AdviceFiles>
        
<AdviceFile>HelloAspectDngAdvice.xml</AdviceFile>
    
</AdviceFiles>
</AspectDngConfig>

HelloAspectDngAdvice.xml

<Advice xmlns="http://www.dotnetguru.org/AspectDNG">
    
<Delete 
        
targetXPath="//Method[@name = 'MethodToBeDeleted']"/>
        
    
<Insert 
        
aspectXPath="//Type[@name = 'TestClassToBeInserted']"
        targetXPath
="//Module"/>

    
<!--<Warning 
        targetXPath="//Method[@name = 'MethodWarningToBeAdded']"/>
-->

    
<!--<Error 
        targetXPath="//Method[@name = 'MethodErrorToBeAdded']"/>
-->
    
    
<!--<MakeSerializable 
        targetXPath="//Method[@name = 'TestBaseClass']"/>
-->
        
    
<!--<SetBaseType 
        aspectXPath="//Type[@name = 'TestClassToBeInserted']"
        targetXPath="//Type[@name = 'TestBaseClass']"/>
-->
    
    
<!--<InlineBeforeConstructorCall 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeConstuctorCall']"
        targetXPath="//TestBaseClass/Constructor"/>
    
    <InlineAfterConstructorCall 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterConstuctorCall']"
        targetXPath="//TestBaseClass/Constructor"/>
    
    <InlineBeforeMethodCall 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeMethodCall']"
        targetXPath="//Method[@name = 'MethodToTestInlineMethod']"/>
    
    <InlineAfterMethodCall 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterMethodCall']"
        targetXPath="//Method[@name = 'MethodToTestInlineMethod']"/>
    
    <InlineBeforeFieldReadAccess 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeFieldReadAccess']"
        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
    
    <InlineAfterFieldReadAccess 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterFieldReadAccess']"
        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
    
    <InlineBeforeFieldWriteAccess 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeFieldWriteAccess']"
        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
    
    <InlineAfterFieldWriteAccess 
        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterFieldWriteAccess']"
        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
-->
        
    
<InlineAtStart 
        
aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAtStart']"
        targetXPath
="//Method[@name = 'MethodToTestInlineMethod']"/>
    
    
<InlineBeforeReturn
        
aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeReturn']"
        targetXPath
="//Method[@name = 'MethodToTestInlineMethod']"/>
    
    
<!--<AroundCall 
        aspectXPath="//Method[@name='CodeToBeAddedAsAroundCall']"
        targetXPath="//Method[@name = 'MethodToTestAroundBody']"/>
-->
    
    
<AroundBody 
        
aspectXPath="//Method[@name='CodeToBeAddedAsAroundBody']"
        targetXPath
="//Method[@name = 'MethodToTestAroundBody']"/>
</Advice>

After execute the command:

aspectdng.exe –w AspectDngConfig.xml

BaseLib.dll is statically weaved and the source code (decompiled by Salamander) becomes below:

// Decompiled by Salamander version 1.0.6
// Copyright 2002 Remotesoft Inc. All rights reserved.
// http://www.remotesoft.com/salamander

using System;

namespace BaseLib
{
  
public class TestBaseClass
  
{
    
private string fieldToTestInlineField = "fieldToTestInlineField";


    
public void MethodWarningToBeAdded()
    
{
    }


    
public void MethodErrorToBeAdded()
    
{
    }


    
public void MethodToTestInlineConstructorCall()
    
{
      
object local = new TestBaseClass();
    }


    
public void MethodToTestInlineMethod()
    
{
      Console.Write(
"CodeToBeAddedAsInlineAtStart");
      Console.Write(
"CodeToBeAddedAsInlineBeforeReturn");
    }


    
public void MethodToTestInlineMethodCall()
    
{
      MethodToTestInlineMethod();
    }


    
public void MethodToTestInlineFieldAccess()
    
{
      
string str = fieldToTestInlineField;
      fieldToTestInlineField 
= "WriteFieldToTestInlineField";
    }


    
public void MethodToTestAroundBody_9e793e7d-2628-4b5d-adf4-c089248c9ee7()
    
{
      Console.Write(
"MethodToTestAroundBody");
    }


    
public void MethodToTestAroundCall()
    
{
      MethodToTestAroundBody();
    }


    
public void MethodToTestAroundBody()
    
{
      Console.Write(
"CodeToBeAddedAsAroundBody");
    }

  }


}

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

<WeaveLog>
  
<Weave name="Delete" targetXPath="//Method[@name = 'MethodToBeDeleted']">
    
<WeaveStep>
      
<TargetInfo>System.Void BaseLib.TestBaseClass::MethodToBeDeleted()</TargetInfo>
    
</WeaveStep>
  
</Weave>
  
<Weave name="Insert" aspectXPath="//Type[@name = 'TestClassToBeInserted']" targetXPath="//Module">
    
<WeaveStep>
      
<AspectInfo>AspectLib.TestClassToBeInserted</AspectInfo>
      
<TargetInfo>BaseLib.dll</TargetInfo>
    
</WeaveStep>
  
</Weave>
  
<Weave name="InlineAtStart" aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAtStart']" targetXPath="//Method[@name = 'MethodToTestInlineMethod']">
    
<WeaveStep>
      
<AspectInfo>System.Void AspectLib.TestAspectClass::CodeToBeAddedAsInlineAtStart()</AspectInfo>
      
<TargetInfo>System.Void BaseLib.TestBaseClass::MethodToTestInlineMethod()</TargetInfo>
    
</WeaveStep>
  
</Weave>
  
<Weave name="InlineBeforeReturn" aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeReturn']" targetXPath="//Method[@name = 'MethodToTestInlineMethod']">
    
<WeaveStep>
      
<AspectInfo>System.Void AspectLib.TestAspectClass::CodeToBeAddedAsInlineBeforeReturn()</AspectInfo>
      
<TargetInfo>System.Void BaseLib.TestBaseClass::MethodToTestInlineMethod()</TargetInfo>
    
</WeaveStep>
  
</Weave>
  
<Weave name="AroundBody" aspectXPath="//Method[@name='CodeToBeAddedAsAroundBody']" targetXPath="//Method[@name = 'MethodToTestAroundBody']">
    
<WeaveStep>
      
<AspectInfo>System.Void AspectLib.TestAspectClass::CodeToBeAddedAsAroundBody()</AspectInfo>
      
<TargetInfo>System.Void BaseLib.TestBaseClass::MethodToTestAroundBody()</TargetInfo>
    
</WeaveStep>
  
</Weave>
</WeaveLog>

Source Code Download

HelloAspectDNG.zip

posted on 2005-09-20 13:15 Teddy's Knowledge Base 阅读(2283) 评论(8)  编辑 收藏 网摘 所属分类: AOSD

评论

#1楼  2005-09-20 13:36 idior      

为什么不同时demo一下在原有类中织入新的方法, 这样全面一点.   回复  引用  查看    

#2楼 [楼主] 2005-09-20 13:41 Teddy's Knowledge Base      

织入新方法和新的类是一样的,只要这样定义Advice就行:
<Insert
aspectXPath="//Type[@name = 'MethodToBeInserted']"
targetXPath="//ClassToAddMethod"/>

因为原来想测试insert一个类,然后再将这个新的类设为原有的一个类的基类的,但是SetBaseType这样Operation似乎支持的还不好,总是设不对,所以最后把SetBaseType给comment了,只保留了这个Instert。
  回复  引用  查看    

#3楼  2005-09-20 14:34 FantasySoft      

So complicated. A little scared. kaka~~~   回复  引用  查看    

#4楼 [楼主] 2005-09-20 18:15 Teddy's Knowledge Base      

修正一些错误,请修改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']"/>-->
  回复  引用  查看    

#5楼 [楼主] 2005-09-21 10:49 Teddy's Knowledge Base      

为了避免AspectDNG现有Configuration的限制和使用上的不方便,我将再下一章中构建一个基于AspectDNG's ILML Library的全新的Apsect Weaver的雏形,之后的AOP Practice章节也将使用这个新的AOP Framework - Teddy's Aspect Weaver,敬请期待!   回复  引用  查看    

#6楼  2006-09-20 21:18 好人 [未注册用户]

我把文件挡下来了,可是不好用呀,哪位大虾指点一下.我是在C#环境下实行的。   回复  引用    

#7楼  2007-03-27 18:35 我是-------蜗牛 [未注册用户]

TEDDY,你好!你水平很好,可是文中还少点讲解心得之类的。这只是我个人的一个小看法(当然主要原因还是因为我刚接触框架之类的,所以一下没看懂,我还会继续看的,有一个Kanas的框架,不知你看过没有),谢谢你的精采献出!   回复  引用    

#8楼  2007-04-16 16:23 肚子饿了      

这种aop的确和应用context的非常的不同,不好理解.   回复  引用  查看    


发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接: