Sunday, November 25, 2007

Background Information

Aspect Oriented Programming has been on my list of things to read up on for almost a year now.  It likely would have stayed on my list of things to do well into the future if it hadn't been for a recent .NET Rocks episode.  I can't recall if it was the Pablo Castro on Astoria, or the Tim Sneath and Ian Ellison show, but one of them off-hand mentioned PostSharp.  As I usually do when links are thrown about on the show, I scribbled it on my hand for later review. Update DNR just released a show with Gael Fraiteur of PostSharp fame!

Once I got to the PostSharp site, I found their sample video on creating a trace AOP attribute.  They had me hooked.  I wasn't so much interested in doing a logging or trace attribute, but was more interested in doing a few validation attributes.  More specifically a NotNullAttribute.

The Problem

What got me interested in doing the NotNullAttribute was thinking back to how many times in our code base we would have something like this:

1 public void AddUser(Organization org, User user) 2 { 3 if (org == null) 4 throw new ArgumentNullException("org"); 5 if (user == null) 6 throw new ArgumentNullException("user"); 7 8 org.AddUser(user); 9 _ordDal.Save(org); 10 }

Granted this is a somewhat short method, but we have methods which take 6 parameters, and only 2 actual lines of code.  That's 12 lines of code taken up for argument checking, and 2 lines of code.  That would put our crap to code ratio at 6/1.

The Solution

Wouldn't it be much nicer if you could do something like this:

1 public void AddUser([NotNull] Organization org, [NotNull] User user) 2 { 3 org.AddUser(user); 4 _ordDal.Save(org); 5 }

 

PostSharp is almost there to let you write code just like that.  Currently, there is no support for an OnParameterAspectBoundaryAttribute in PostSharp, so we have to do things just slightly differently:

[NotNullHelper] public void AddUser([NotNull] Organization org, [NotNull] User user) { org.AddUser(user); _ordDal.Save(org); }

We have to have a helper class were all the actual code lives.

How PostSharp Works

PostSharp works by weaving all your AOP attributes with your source code after the compiler compiles your code.  This article by Gael Fraiteur describes in more detail how the PostSharp process works.  After the code is post-compiled by PostSharp, you end up with something very ugly that looks like this:

public void AddUser([NotNull] Organization org, [NotNull] User user) { MethodExecutionEventArgs ~laosEventArgs~2; try { object[] ~arguments~1 = new object[] { org, user }; ~laosEventArgs~2 = new MethodExecutionEventArgs(methodof(OrgManager.AddUser, OrgManager), this, ~arguments~1); ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnEntry(~laosEventArgs~2); if (~laosEventArgs~2.FlowBehavior != FlowBehavior.Return) { org.AddUser(user); _orgDal.Save(org); ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnSuccess(~laosEventArgs~2); } } catch (Exception ~exception~0) { ~laosEventArgs~2.Exception = ~exception~0; ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnException(~laosEventArgs~2); switch (~laosEventArgs~2.FlowBehavior) { case FlowBehavior.Continue: case FlowBehavior.Return: return; } throw; } finally { ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnExit(~laosEventArgs~2); } }

I don't know about you, but I would have one HELL of a time trying to debug this, thankfully PostSharp touches up the PDB files, so when your debugging, you only see and debug the source code you've written, both your attribute, and your business logic.

The Drawbacks

PostSharp modifies your code! Well, not directly, but it is still modifying your code (as seen above), but thankfully it touches up the PDB files so all the mess under the hood is hidden from you.

Conclusion

This is but one of the many things you can do with AOP and PostSharp.  Next up, I plan to write some validation attributes.  Look for those in the next few weeks.  In the mean time, download the source code for the NotNullAttribute.

I'm sorry if this post looks terrible in your feed reader, I still haven't been able to figure out why the code looks terrible.  If anyone has any suggestions, let me know.  I'm using DasBlogCE as my blog engine.

kick it on DotNetKicks.com
posted on Sunday, November 25, 2007 8:37:23 PM (Alaskan Standard Time, UTC-09:00)  #    Comments [3]
Related posts:
LINQ Goodness
Creating Windows Firewall Exceptions
Problems serializing class which inherits from a generic list
ASP.NET Resource Refactoring
Using Custom Attributes to Enable Quick Searching of Your Domain Entities With NHibernate
NHibernate, DateTime and UTC

Referred by:
http://www.dotnetkicks.com/architecture/De_crapify_your_code... [Referral]
http://www.dotnetkicks.com/ [Referral]
http://www.dotnetkicks.com/page/3 [Referral]
postsharp debugging (www.google.com) [Referral]
postsharp (search.live.com) [Referral]
http://colinjack.blogspot.com/2007/11/aop-alternatives-to-po... [Referral]
AOP using in asp.Net (www.google.co.in) [Referral]
AOP using PostSharp .Net (www.google.com) [Referral]
postsharp debugging (www.google.co.uk) [Referral]
postsharp NotNullAttribute (www.google.com) [Referral]
google code postsharp (www.google.nl) [Referral]
aop vs tracing c# (www.google.ca) [Referral]
postsharp debugging (www.google.pl) [Referral]
PostSharp debug (www.google.ru) [Referral]
postsharp pdb (www.google.com) [Referral]
PostSharp Attribute Adding (www.google.co.nz) [Referral]
postsharp vs nhibernate (www.google.com.ua) [Referral]
postsharp notnull (www.google.com.au) [Referral]
notnull aspect postsharp (www.google.com) [Referral]
http://www.dotnetkicks.com/tags/AOP [Referral]
C# postsharp review (www.google.com) [Referral]
postsharp exit code 12 (www.google.com) [Referral]
postsharp attribute (www.google.ca) [Referral]
postsharp (www.google.com) [Referral]
aop postsharp (www.google.nl) [Referral]
postsharp debugging (www.google.com.tr) [Referral]
using postsharp with ASP.NET (www.google.ca) [Referral]
http://colinjack.blogspot.com/search?updated-min=2007-01-01T... [Referral]
postsharp attribute not called (www.google.de) [Referral]
postsharp debug (www.google.fr) [Referral]
Postsharp debugging (www.google.co.in) [Referral]
postsharp attribute argument (www.google.be) [Referral]
postsharp ayende (www.google.nl) [Referral]
http://colinjack.blogspot.com/search/label/AOP [Referral]
c# postsharp (www.google.ro) [Referral]
using postsharp (www.google.co.uk) [Referral]
postsharp (search.live.com) [Referral]
validators postsharp (www.google.com) [Referral]
Postsharp validation (www.google.com.au) [Referral]
postsharp debugging (www.google.hu) [Referral]
postsharp review (www.google.com) [Referral]
NotNullAttribute C# (www.google.com) [Referral]
aop code validation (www.google.fr) [Referral]
business base postsharp (www.google.ch) [Referral]
validation with postsharp (www.google.com) [Referral]
c# notnullattribute (www.google.de) [Referral]
postsharp laos asp.net (www.google.be) [Referral]
postsharp asp.net (www.google.de) [Referral]
postsharp in asp.net (www.google.hu) [Referral]
debug postsharp (www.google.co.uk) [Referral]
postsharp Nhibernate (www.google.de) [Referral]
Postsharp notnull (www.google.com) [Referral]
aop (www.dotnetkicks.com) [Referral]
NotNullAttribute (search.ie7pro.com) [Referral]
PostSharp in ASP.NET (www.google.com) [Referral]
google postsharp (www.google.ca) [Referral]
postsharp debug (www.google.com) [Referral]
http://del.icio.us/tag/postsharp?setcount=100 [Referral]
FlowBehavior PostSharp (www.google.com) [Referral]
postsharp linq (www.google.com) [Referral]
PostSharp debugging (www.google.ru) [Referral]
PostSharp MethodExecutionEventArgs (www.google.de) [Referral]
flowbehavior postsharp (www.google.com) [Referral]
notnullattribute (www.google.ru) [Referral]
postsharp notnull (www.google.nl) [Referral]
c# aop vs attributes 2008 (www.google.com) [Referral]
postsharp "exit code 12" (www.google.com) [Referral]
PostSharp Review (www.google.com) [Referral]
aop attribute (www.google.ru) [Referral]
postsharp aop[ (www.google.com) [Referral]
aop weaving with using source code (www.google.ca) [Referral]
postsharp argument validation (www.google.com) [Referral]
postsharp nhibernate (www.google.es) [Referral]
Postsharp nhibernate (www.google.com) [Referral]
postsharp aop (search.live.com) [Referral]
postsharp laos com c# (www.google.com.br) [Referral]
postsharp nhibernate (www.google.ch) [Referral]
postsharp c# (www.google.com) [Referral]
postsharp review (www.google.co.uk) [Referral]
postsharp exit code 12 (www.google.com) [Referral]
postsharp exit code 12 (www.google.co.uk) [Referral]
AOP+validation code (www.google.com) [Referral]
validation postsharp (www.google.com) [Referral]
PostSharp + .NET 1.1 (www.google.com) [Referral]
postsharp debugging (www.google.com) [Referral]
postsharp exit code 12 (www.google.com) [Referral]
AOP NHibernate (www.google.com) [Referral]
postsharp asp.net (www.google.com) [Referral]
using postsharp (www.google.com) [Referral]
postsharp linq (www.google.co.uk) [Referral]
PostSharp rocks (www.google.com) [Referral]
c# AOP validation (www.google.it) [Referral]
AOP "attributes" (www.google.co.il) [Referral]
postsharp validation (www.google.com) [Referral]
Validation postsharp (www.google.de) [Referral]
NotNullHelper (www.google.de) [Referral]
"exit code 12" PostSharp (www.google.com.br) [Referral]
postsharp c# custom (www.google.com) [Referral]
postsharp alternatives (www.google.com) [Referral]
PostSharp for Linq entities (www.google.co.uk) [Referral]
notnullattribute postsharp (www.google.com.tr) [Referral]
postsharp laos validate (www.google.com) [Referral]
notnullattribute (www.google.com) [Referral]
postsharp review (www.google.no) [Referral]
PostSharp notnull (www.google.com.ua) [Referral]
PostSharp notnull attribute (www.google.com.ua) [Referral]
PostSharp notnull attribute (www.google.com.ua) [Referral]
using postsharp for catching exceptions (www.google.pl) [Referral]
notnullattribute (www.google.com) [Referral]
postsharp code 12 (www.google.co.uk) [Referral]
postsharp with nhibernate (www.google.com) [Referral]
postsharp debugging (www.google.com) [Referral]
validation using postsharp (www.google.com) [Referral]
postsharp (search.live.com) [Referral]
validation postsharp (www.google.co.za) [Referral]
+postsharp +validation (www.google.com.au) [Referral]
postsharp aop (www.google.co.za) [Referral]
postsharp review (www.google.co.uk) [Referral]
info:http://www.milkcarton.com/blog/2007/11/26/Decrapify%20Your%20Code%20Base%20With%20AOP%20Using%20PostSharp.aspx (toolbarqueries.google.com) [Referral]
postsharp debug (www.google.de) [Referral]
aop validation c# (www.google.com) [Referral]
postsharp hanselman (www.google.com) [Referral]
PostSharp .NET 1.1 (www.google.de) [Referral]
PostSharp Exceptions examples (www.google.ca) [Referral]
notnullattribute (www.google.com) [Referral]
postsharp debugging (www.google.co.uk) [Referral]
generic and laos postsharp (www.google.com) [Referral]
c# AOP (www.google.ro) [Referral]
postsharp validation (www.google.com) [Referral]
postsharp (www.google.cn) [Referral]
C# notnullattribute (www.google.co.jp) [Referral]
postsharp flowbehavior (www.google.com) [Referral]
NotNullAttribute (www.google.ca) [Referral]
souce code, toolbarqueries.google.com (www.google.com) [Referral]
c# aop tracing (www.google.it) [Referral]
postsharp validation (www.google.cz) [Referral]
postsharp il (www.google.si) [Referral]
debug postsharp code (www.google.com) [Referral]
postsharp parameter not null (www.google.com) [Referral]
.net aop validation (www.google.com) [Referral]
postsharp notnull (www.google.com) [Referral]
postsharp validator (www.google.it) [Referral]
NotNullAttribute (www.google.ca) [Referral]
postsharp review (www.google.com) [Referral]
aop .net 1.1 (www.google.co.uk) [Referral]
postsharp crappy (www.google.com) [Referral]
PostSharp review (www.google.com) [Referral]
validation postsharp code project (www.google.nl) [Referral]
postsharp asp.net (www.google.com) [Referral]
Using Postsharp (www.google.com) [Referral]
postsharp debug method arguments (www.google.com) [Referral]
postsharp nhibernate (www.google.com) [Referral]
postsharp exitcode 12 (www.google.fr) [Referral]
postsharp validation (www.google.cz) [Referral]
postsharp asp.net (www.google.com) [Referral]
postsharp review (www.google.com) [Referral]
postsharp orm (www.google.ch) [Referral]
c# aop (www.google.ca) [Referral]
postsharp validation (www.google.cz) [Referral]
aop asp.net (search.yahoo.com) [Referral]
postsharp return code (www.google.de) [Referral]
postsharp review (www.google.ca) [Referral]
PostSharp NHibernate (www.google.ca) [Referral]
postsharp validation (www.google.cz) [Referral]
postsharp + catching exception (www.google.com) [Referral]
postsharp "exit code 12" debug (www.google.cz) [Referral]
"how postsharp works" (www.google.co.il) [Referral]
postsharp debug (www.google.cz) [Referral]
postsharp orm (www.google.com) [Referral]
c# aop tracing (www.google.com) [Referral]
postsharp sample (www.google.ca) [Referral]
postsharp examples (www.google.com) [Referral]
postsharp review (www.google.co.uk) [Referral]
C# AOP Tracing (www.google.co.th) [Referral]
c# aop logging attribute (www.google.dk) [Referral]
postsharp validation (www.google.fr) [Referral]
postsharp throws exceptions (www.google.nl) [Referral]
debugging postsharp (www.google.com) [Referral]
postsharp validator (www.google.com.tr) [Referral]
postsharp trace (www.google.com) [Referral]
postsharp debugging (www.google.co.uk) [Referral]
postsharp on .net 1.1 (www.google.es) [Referral]
PostSharp parameter check null (www.google.com) [Referral]
PostSharp NotNullHelper (www.google.com) [Referral]
PostSharp AOP (www.google.ru) [Referral]
nhibernate AOP (www.google.ca) [Referral]
c# attributes aop (www.google.com) [Referral]
ayende aop mess code (www.google.pl) [Referral]
PostSharp examples (www.google.nl) [Referral]
aop parameter validators (www.google.com) [Referral]
PostSharp nhibernate (www.google.co.uk) [Referral]
postsharp adding attributes (www.google.com) [Referral]
review postsharp (www.google.be) [Referral]
postsharp vs aspect# (www.google.com) [Referral]
postsharp exit code 12 (www.google.ru) [Referral]
linq postsharp (www.google.com) [Referral]
anyone using postsharp (www.google.com) [Referral]
validations postsharp (www.google.es) [Referral]
postsharp validation (www.google.ca) [Referral]
postsharp review (www.google.co.uk) [Referral]
http://www.dotnetkicks.com/tags/Architecture/page/27 [Referral]
postsharp notnull (www.google.com) [Referral]
postsharp pdb (www.google.com) [Referral]
postsharp notnull (www.google.com) [Referral]
+Postsharp +Non +Null (www.google.ru) [Referral]
http://g.s.scandoo.com/7BB3AEA600532F2111B23633D64921EE.cach... [Referral]
http://delicious.com/tag/postsharp?page=3 [Referral]
postsharp debugging (www.google.com) [Referral]
methodof postsharp (www.google.com) [Referral]
notnullattribute (www.google.com) [Referral]
postsharp orm (www.google.se) [Referral]
postsharp review (www.google.com.br) [Referral]
MethodExecutionEventArgs c# postsharp arguments (www.google.be) [Referral]
aop arguments validation (www.google.de) [Referral]
postsharp validatie (www.google.be) [Referral]
using postsharp c# (www.google.be) [Referral]
postsharp exceptions (www.google.be) [Referral]
postsharp nhibernate (www.google.com) [Referral]
postsharp parameter (www.google.com) [Referral]
postsharp exception handling (www.google.co.nz) [Referral]
postsharp FlowBehavior (search.live.com) [Referral]
postsharp method parameter (www.google.com) [Referral]
notnullattribute c# (www.google.com) [Referral]
debug postsharp attribute (www.google.com.br) [Referral]
debug postsharp (www.google.com) [Referral]
C# AOP validation (www.google.com) [Referral]
flowbehavior postsharp (www.google.com.br) [Referral]
postsharp validation (www.google.ru) [Referral]
postsharp debug (www.google.de) [Referral]
postsharp nhibernate (www.google.com) [Referral]
exceptions handling in postsharp (www.google.com) [Referral]
postsharp flowbehavior (www.google.de) [Referral]
postsharp check parameter (www.google.com) [Referral]
.net c# argument validation (www.google.com) [Referral]
postsharp generic asspect (www.google.ca) [Referral]
postsharp parameter validation (www.google.com) [Referral]
how works postsharp (www.google.com) [Referral]
c# aop (www.google.com) [Referral]
postsharp review (www.google.md) [Referral]
notnullattribute c# (www.google.co.uk) [Referral]
Postsharp Exit code 12 (www.google.com) [Referral]
postsharp c# AOP (www.google.com) [Referral]
aop logging c# (www.google.co.uk) [Referral]
postsharp nhibernate (www.google.ca) [Referral]
postsharp notnull (www.google.co.nz) [Referral]
postsharp versus aspect# (www.google.com) [Referral]
c# tracing with aop (www.google.com.br) [Referral]
c# aop 2008 (www.google.co.za) [Referral]
c# postsharp (www.google.co.za) [Referral]
aspx postsharp (www.google.it) [Referral]
using exceptions for validation c# (www.google.co.uk) [Referral]
C# postsharp examples (www.google.com) [Referral]
postsharp debugging (www.google.nl) [Referral]
postsharp example (www.google.com) [Referral]
postsharp exception (www.google.com) [Referral]
postsharp exception handling (www.google.com) [Referral]
PostSharp for .asp.net (search.yahoo.com) [Referral]
asp.net postsharp (www.google.pl) [Referral]
postsharp debugging (www.google.com) [Referral]
postsharp samples (search.live.com) [Referral]
postsharp nhibernate (www.google.com.tw) [Referral]
debugging postsharp (www.google.com) [Referral]
aop logging c# (www.google.se) [Referral]
postsharp validation (www.google.com.au) [Referral]
postsharp nhibernate (www.google.co.uk) [Referral]
http://colinjack.blogspot.com/search/label/DDD [Referral]
c# aop attributes (www.google.co.uk) [Referral]
postsharp exit code 12 (www.google.it) [Referral]
postsharp exitcode 12 (www.google.it) [Referral]
C# AOP logging (www.google.com) [Referral]
aop logging attribute (www.google.ca) [Referral]
.net attribute logging AOP postsharp (www.google.ca) [Referral]
C# AOP logging (www.google.com) [Referral]
postsharp weaving code (www.google.de) [Referral]
AOP validation C# (www.google.com.au) [Referral]
validator attribute postsharp (www.google.co.ug) [Referral]
PostSharp.AspNet download (www.google.it) [Referral]
postsharp server 2003 (www.google.com) [Referral]
postsharp nhibernate (www.google.fi) [Referral]
PostSharp NHibernate (www.google.com) [Referral]
attribute postsharp c# validation (www.google.co.ug) [Referral]
.Net AOP Tracing (www.google.co.uk) [Referral]
postsharp linq (www.google.com) [Referral]
postsharp nhibernate (www.google.com) [Referral]
postsharp validation (www.google.ru) [Referral]
postsharp null validation (www.google.co.za) [Referral]
debug postsharp (www.google.de) [Referral]
postsharp debugging (www.google.co.uk) [Referral]
aop validators (www.google.com) [Referral]
postsharp parameter (www.google.com) [Referral]
postsharp review (www.google.com) [Referral]
+postsharp nhibernate (www.google.com) [Referral]
c# notnullattribute (www.google.ru) [Referral]
postsharp debug (www.google.com.mx) [Referral]
postsharp code 12 (www.google.it) [Referral]
using postsharp exception (www.google.co.uk) [Referral]
C# AOP logging (www.google.com) [Referral]
postsharp nhibernate (www.google.pl) [Referral]
PostSharp.AspNet (www.google.it) [Referral]
postsharp flowbehaviour (www.google.com) [Referral]
attribute aop asp.net (www.google.com.ua) [Referral]
c# aop exception architecture (www.google.com) [Referral]
PostSharp.AspNet (www.google.com.br) [Referral]
exit code 12 postsharp (www.google.ca) [Referral]
postsharp.aspnet (www.google.com.br) [Referral]
PostSharp user samples (www.google.com) [Referral]
FlowBehavior postsharp (www.google.co.in) [Referral]
PostSharp exit code 12 (www.google.co.il) [Referral]
ayende aop (www.google.com) [Referral]
PostSharp asp.net web site (www.google.com) [Referral]
PostSharp logging (www.google.com) [Referral]
google.pl/referral (www.google.si) [Referral]
postsharp flowbehaviour (www.google.co.uk) [Referral]
postsharp review (www.google.com) [Referral]
postsharp validation (www.google.com) [Referral]
postsharp.aspnet (www.google.com) [Referral]
Postsharp "exit code 12" (www.google.com) [Referral]
adding base 8 base 20 to c# (search.live.com) [Referral]
example of postsharp for exception (www.google.co.in) [Referral]
postsharp asp.net (www.google.fi) [Referral]
debugging with postsharp (www.google.com) [Referral]
postsharp c# exception (www.google.nl) [Referral]
PostSharp error handling (www.google.com.au) [Referral]
notnull .net postsharp (www.google.pl) [Referral]
postsharp exit code 12 (www.google.com) [Referral]
PostSharp C# (www.google.pt) [Referral]
postsharp debug (www.google.pt) [Referral]
AOP logging C# (www.google.com) [Referral]
postsharp validation (www.google.pl) [Referral]
postsharp AspNet (www.google.com) [Referral]
AOP (www.dotnetkicks.com) [Referral]
postsharp notnull (www.google.com) [Referral]
postsharp validation (www.google.dk) [Referral]
PostSharp C# (www.google.fi) [Referral]
postsharp +debugging (www.google.com) [Referral]
postsharp examples (www.google.com) [Referral]
http://us.mg2.mail.yahoo.com/dc/blank.html?bn=1155.45&.intl=... [Referral]
Postsharp review (www.google.com) [Referral]
postsharp asp.net (www.google.com) [Referral]
postsharp CA attribute (www.google.pl) [Referral]
download code for AOP in asp.net (www.google.com) [Referral]
Monday, November 26, 2007 9:53:25 PM (Alaskan Standard Time, UTC-09:00)
I want to undrestand can i use this code in c#2003
Tuesday, November 27, 2007 8:08:00 AM (Alaskan Standard Time, UTC-09:00)
Nice article. Hell, even if the article sucked, I just love the title. And I love the concept of identifying crap to code ratios.

cheers,

Trevor

Trevor
Tuesday, November 27, 2007 9:23:41 AM (Alaskan Standard Time, UTC-09:00)
Amir,
I don't believe it works under .NET 1.1
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview