<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Freekshow &#187; AOP</title>
	<atom:link href="http://freekleemhuis.com/category/aop/feed/" rel="self" type="application/rss+xml" />
	<link>http://freekleemhuis.com</link>
	<description>SoftWear 'n Tears</description>
	<lastBuildDate>Wed, 05 May 2010 16:44:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='freekleemhuis.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4539f785b4d546832e4e6a93f0d1a38f?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Freekshow &#187; AOP</title>
		<link>http://freekleemhuis.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://freekleemhuis.com/osd.xml" title="Freekshow" />
	<atom:link rel='hub' href='http://freekleemhuis.com/?pushpress=hub'/>
		<item>
		<title>PostSharp: AOP for .Net</title>
		<link>http://freekleemhuis.com/2008/10/02/postsharp-aop-for-net/</link>
		<comments>http://freekleemhuis.com/2008/10/02/postsharp-aop-for-net/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 10:10:15 +0000</pubDate>
		<dc:creator>Freek Leemhuis</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://freekleemhuis.wordpress.com/?p=153</guid>
		<description><![CDATA[The updates to Microsoft&#8217;s reference architecture (find them on www.codeplex.com/AppArch) got me thinking about what a good reference implementation would be when adopting Domain Driven Design. For now let&#8217;s focus on cross-cutting concerns, as they are depicted in the reference architecture.   The cannonical example for a cross-cutting concern is logging, and I&#8217;ve come across quite [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=freekleemhuis.com&amp;blog=2670743&amp;post=153&amp;subd=freekleemhuis&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharp1.jpg"></a>The updates to Microsoft&#8217;s reference architecture (find them on <a href="http://www.codeplex.com/AppArch">www.codeplex.com/AppArch</a>) got me thinking about what a good reference implementation would be when adopting Domain Driven Design.</p>
<p>For now let&#8217;s focus on <a href="http://en.wikipedia.org/wiki/Cross-cutting_concern" target="_blank">cross-cutting concerns</a>, as they are depicted in the reference architecture.</p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/webapp.jpg"><img class="alignnone size-full wp-image-154" title="webapp" src="http://freekleemhuis.files.wordpress.com/2008/10/webapp.jpg?w=515&#038;h=622" alt="" width="515" height="622" /></a></p>
<p> </p>
<p>The cannonical example for a cross-cutting concern is logging, and I&#8217;ve come across quite a few applications that had logging code scattered across the entire application. A better way to separate these concerns is by using <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank">Aspect Oriented Programming</a> (AOP). In the .Net world there&#8217;s only <a href="http://www.codeproject.com/KB/cs/AOP_Frameworks_Rating.aspx" target="_blank">a few frameworks that deal with AOP</a>, and of these PostSharp is probably best known. The Enterprise Library ofcourse has the <a href="http://msdn.microsoft.com/en-us/library/cc511694.aspx" target="_blank">Policy Injection Application Block</a>, which has similar functionality.</p>
<p>I have been spending some time with PostSharp, and I really like the improvement it can bring in the application design. And it&#8217;s really not hard to do. I&#8217;ll give a quick example: </p>
<p>After you&#8217;ve installed the PostSharp bits you&#8217;ll need to include two references, to PostSharp.Laos and PostSharp.Public.</p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharp1.jpg"><img class="alignnone size-full wp-image-158" title="postsharp1" src="http://freekleemhuis.files.wordpress.com/2008/10/postsharp1.jpg?w=482&#038;h=406" alt="" width="482" height="406" /></a></p>
<p>First, you wil need to create an aspect class, let&#8217;s name it TraceAspect.</p>
<pre class="brush: csharp;">[Serializable]
public sealed class TraceAspect : OnMethodBoundaryAspect       
{
    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
       Console.WriteLine(&quot;User {0} entering method {1} at {2}&quot; , Environment.UserName, eventArgs.Method , DateTime.Now.ToShortTimeString());           
    }
    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
       Console.WriteLine(&quot;User {0} exiting method {1} at {2}&quot;, Environment.UserName, eventArgs.Method, DateTime.Now.ToShortTimeString());
    }
}</pre>
<p>A few things to note here:</p>
<ul>
<li>You will need to mark the class with the Serializable attribute.</li>
<li>The OnMethodBoundaryAspect actually extends the Attibute class, so it&#8217;s like you&#8217;re creating a custom Attribute.</li>
<li>We&#8217;re implementing the aspect handlers by overriding the designated base class handlers, in this case OnEntry and OnExit.</li>
</ul>
<p>Were now ready to apply the postsharp attribute to our classes to add tracing</p>
<pre class="brush: csharp;">class Program
    {
        static void Main(string[] args)
        {
            Customer cust = GetCustomer(1);
            SaveCustomer(cust);
            Console.ReadKey();
        }

        [TraceAspect]
        public static Customer  GetCustomer(int customerId)
        {
            Console.WriteLine(&quot;GetCustomer is executing&quot;);
            System.Threading.Thread.Sleep(2000); //taking it's time...
            return new Customer { FirstName = &quot;Private&quot;, LastName = &quot;Ryan&quot; };
        }
        [TraceAspect]
        public static void SaveCustomer(Customer customer)
        {
            Console.WriteLine(&quot;SaveCustomer is executing&quot;);
            System.Threading.Thread.Sleep(3000); //taking it's time... again...
        }

        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }</pre>
<p>As you can see ,there&#8217;s not much to it but adding the [TraceAspect] attribute to our class. When executing this little sample you&#8217;ll get the expected output:</p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharp2.jpg"><img class="alignnone size-full wp-image-162" title="postsharp2" src="http://freekleemhuis.files.wordpress.com/2008/10/postsharp2.jpg?w=677&#038;h=180" alt="" width="677" height="180" /></a></p>
<p>The magic is performed for us here by invoking the post-compiler that it uses to weave the AO code into the IL</p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharptransform.jpg"><img class="alignnone size-full wp-image-165" title="postsharptransform" src="http://freekleemhuis.files.wordpress.com/2008/10/postsharptransform.jpg?w=462&#038;h=167" alt="" width="462" height="167" /></a></p>
<p>So that when you use <a href="http://reflector.red-gate.com/" target="_blank">Reflector</a> (I almost added Lutz&#8217;s name here&#8230;) to look at your final assembly, you can see the weaving that has been done:</p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharpweaving.jpg"><img class="alignnone size-full wp-image-166" title="postsharpweaving" src="http://freekleemhuis.files.wordpress.com/2008/10/postsharpweaving.jpg?w=993&#038;h=600" alt="" width="993" height="600" /></a></p>
<p>PostSharp is a very powerful tool and it&#8217;s capable of a lot more than what I can show here. It&#8217;s a bit of a one-man band behind it, but it&#8217;s creator <a href="http://gael.fraiteur.net/" target="_blank">Gael Fraiteur</a> has done a fine job of releasing it to open source and it&#8217;s actually got good<a href="http://www.postsharp.org/about/documentation/" target="_self"> documentation</a>. Check it out on <a href="http://www.postsharp.org">www.postsharp.org</a></p>
<p> </p>
<p><a href="http://freekleemhuis.files.wordpress.com/2008/10/postsharp-transform.jpg"></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/freekleemhuis.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/freekleemhuis.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/freekleemhuis.wordpress.com/153/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=freekleemhuis.com&amp;blog=2670743&amp;post=153&amp;subd=freekleemhuis&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://freekleemhuis.com/2008/10/02/postsharp-aop-for-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bfef0fe08324b3d16ab57b6502d2c3b3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Freek Leemhuis</media:title>
		</media:content>

		<media:content url="http://freekleemhuis.files.wordpress.com/2008/10/webapp.jpg" medium="image">
			<media:title type="html">webapp</media:title>
		</media:content>

		<media:content url="http://freekleemhuis.files.wordpress.com/2008/10/postsharp1.jpg" medium="image">
			<media:title type="html">postsharp1</media:title>
		</media:content>

		<media:content url="http://freekleemhuis.files.wordpress.com/2008/10/postsharp2.jpg" medium="image">
			<media:title type="html">postsharp2</media:title>
		</media:content>

		<media:content url="http://freekleemhuis.files.wordpress.com/2008/10/postsharptransform.jpg" medium="image">
			<media:title type="html">postsharptransform</media:title>
		</media:content>

		<media:content url="http://freekleemhuis.files.wordpress.com/2008/10/postsharpweaving.jpg" medium="image">
			<media:title type="html">postsharpweaving</media:title>
		</media:content>
	</item>
	</channel>
</rss>