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

“Hello World” Example with AspectDNGusing System;
“Hello World” Example with AspectDNG
“Hello World” Example with AspectDNG
namespace BaseLib

TestAspectClass.cs in AspectLib.dll

“Hello World” Example with AspectDNGusing System;
“Hello World” Example with AspectDNG
“Hello World” Example with AspectDNG
namespace AspectLib

The AspectDngConfig and Advice configuration are as below:

AspectDngConfig.xml

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

HelloAspectDngAdvice.xml

“Hello World” Example with AspectDNG<Advice xmlns="http://www.dotnetguru.org/AspectDNG">
“Hello World” Example with AspectDNG    
<Delete 
“Hello World” Example with AspectDNG        
targetXPath="//Method[@name = 'MethodToBeDeleted']"/>
“Hello World” Example with AspectDNG        
“Hello World” Example with AspectDNG    
<Insert 
“Hello World” Example with AspectDNG        
aspectXPath="//Type[@name = 'TestClassToBeInserted']"
“Hello World” Example with AspectDNG        targetXPath
="//Module"/>
“Hello World” Example with AspectDNG
“Hello World” Example with AspectDNG    
<!--<Warning 
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'MethodWarningToBeAdded']"/>
-->
“Hello World” Example with AspectDNG
“Hello World” Example with AspectDNG    
<!--<Error 
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'MethodErrorToBeAdded']"/>
-->
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    
<!--<MakeSerializable 
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'TestBaseClass']"/>
-->
“Hello World” Example with AspectDNG        
“Hello World” Example with AspectDNG    
<!--<SetBaseType 
“Hello World” Example with AspectDNG        aspectXPath="//Type[@name = 'TestClassToBeInserted']"
“Hello World” Example with AspectDNG        targetXPath="//Type[@name = 'TestBaseClass']"/>
-->
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    
<!--<InlineBeforeConstructorCall 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeConstuctorCall']"
“Hello World” Example with AspectDNG        targetXPath="//TestBaseClass/Constructor"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineAfterConstructorCall 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterConstuctorCall']"
“Hello World” Example with AspectDNG        targetXPath="//TestBaseClass/Constructor"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineBeforeMethodCall 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeMethodCall']"
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'MethodToTestInlineMethod']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineAfterMethodCall 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterMethodCall']"
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'MethodToTestInlineMethod']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineBeforeFieldReadAccess 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeFieldReadAccess']"
“Hello World” Example with AspectDNG        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineAfterFieldReadAccess 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterFieldReadAccess']"
“Hello World” Example with AspectDNG        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineBeforeFieldWriteAccess 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeFieldWriteAccess']"
“Hello World” Example with AspectDNG        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    <InlineAfterFieldWriteAccess 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAfterFieldWriteAccess']"
“Hello World” Example with AspectDNG        targetXPath="//Field[@name = 'fieldToTestInlineField']"/>
-->
“Hello World” Example with AspectDNG        
“Hello World” Example with AspectDNG    
<InlineAtStart 
“Hello World” Example with AspectDNG        
aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineAtStart']"
“Hello World” Example with AspectDNG        targetXPath
="//Method[@name = 'MethodToTestInlineMethod']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    
<InlineBeforeReturn
“Hello World” Example with AspectDNG        
aspectXPath="//Method[@name = 'CodeToBeAddedAsInlineBeforeReturn']"
“Hello World” Example with AspectDNG        targetXPath
="//Method[@name = 'MethodToTestInlineMethod']"/>
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    
<!--<AroundCall 
“Hello World” Example with AspectDNG        aspectXPath="//Method[@name='CodeToBeAddedAsAroundCall']"
“Hello World” Example with AspectDNG        targetXPath="//Method[@name = 'MethodToTestAroundBody']"/>
-->
“Hello World” Example with AspectDNG    
“Hello World” Example with AspectDNG    
<AroundBody 
“Hello World” Example with AspectDNG        
aspectXPath="//Method[@name='CodeToBeAddedAsAroundBody']"
“Hello World” Example with AspectDNG        targetXPath
="//Method[@name = 'MethodToTestAroundBody']"/>
“Hello World” Example with AspectDNG
</Advice>

After execute the command:

aspectdng.exe –w AspectDngConfig.xml

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

“Hello World” Example with AspectDNG

“Hello World” Example with AspectDNG// Decompiled by Salamander version 1.0.6
“Hello World” Example with AspectDNG
// Copyright 2002 Remotesoft Inc. All rights reserved.
“Hello World” Example with AspectDNG
// http://www.remotesoft.com/salamander
“Hello World” Example with AspectDNG

“Hello World” Example with AspectDNG
using System;
“Hello World” Example with AspectDNG
“Hello World” Example with AspectDNG
namespace BaseLib

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

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

Source Code Download

HelloAspectDNG.zip

相关文章: