I am Still Alive


Yes you heard me, I am still alive and well in Cyber space,

Why the I’m still alive quote, well it’s got to be about 2 months since I had chance to post anything, and for good reason, I’ve been very busy on some recent contracting projects and various other bits and pieces, and as many of you will also now see I’ve moved my blog from MSN-Live space to wordpress. I’ve also been busy establishing a presence on Twitter “@shawty_ds” if anyone cares to follow me, and as any of you who are avid Lidnug followers will know, I’ve been busy with the rest of the team building the new web site.

So, did i do this post just to tell you I was still here, we’ll yes and no. I’ve been playing with something new latley, a new programming methology called AOP or “Aspect Orientated Programming”, and I’ve got to say I’ve taken rather a shine to it.

so what is AOP I hear you cry, well AOP is to most of you the process of using Attributes in your .NET code. Most of you who have seen these will have been using things like security in an ASP.NET MVC project eg:

[HttpPost]
[Secure]
public ActionResult index()
{
return View();
}

 

These items in the Square brackets that make sure an action can only be called by post, or that call the various routines to check against memberships and roles for authentication are the A in AOP.

Aspect orientated programming is used where any common piece of code consistantly and without change, cross cut’s a piece of code that forms core buisness logic in any code that a project uses, let’s consider the following common example:

public transferFunds(BankAccount source, BankAccount dest, decimal Amount)
{
withdraw(source, Amount);
deposit(dest, Amount);
}

 

That code as it stands, is a perfect example of a business rule defined in a banking application let’s say, nothing complicated and all very very straight forward, but there’s somthing missing.

First off, there’s no validation, there’s no logging or anything like that, just a nice clean business rule. Ok, so lets add in the stuff I didn’t put in first time round.

public transferFunds(BankAccount source, BankAccount dest, decimal Amount)
{
if(source.funds() < Amount)
throw(Exception(“Not Enough Funds!”);
logTransaction(“Money transfered from {0} to {1} (Amount = {2})”,source.accountNum, dest.accountNum, Amount);
withdraw(source, Amount);
deposit(dest, Amount);
}

 

Now while this is not a complicated example, just imagine scaling this up to a full size application, transferFunds, getFunds, depositFunds and maybe a thousand other methods and routines, ALL of which you have to add the same (or very simmilar) validation and logging code too.

Now you could create utility functions in seperate classes, but that still entails making calls like

isAuthorised = checkFunds(Account);

 

Instead, it looks much nicer to be able to use somthing like

[checkFunds]
[logTransaction]
public transferFunds(BankAccount source, BankAccount dest, decimal Amount)
{
withdraw(source, Amount);
deposit(dest, Amount);
}

 

and… hey presto we have our lovley clean business rule only code with us once again, but we still have the ability to tag on common code (Which by the way now ONLY needs to be written out once) and still maintain nice neat readable sources.

So you may ask, what put me onto this, and why have you not showed us how the implementation works yet shawty?

well, I was talking to one of the Lidnug moderators a few weeks ago and he mentioned a product that he was working with called “Post Sharp”, at first when he tried to explain what AOP was about, I like many didn’t quite see the advantage, but as many of you know I’m a big fan of the MVC style of working and esp the new .NET ASP MVC framework, and well it wasn’t long before I started to spot AOP at work and quite extensively within.

As I mentioned just above, the Post and Security attributes are AOP style, and many other elements of the ASP MVC framework are too.

So I downloaded “Post Sharp” from the SharpCrafters website (You can get a Free community version here) to try out and have a play with, which makes implementing your own AOP style attributes in your own projects a breeze. I have to confess, I’ve not done any major stuff with it yet, but I do plan to have a real good play with it over the coming weeks and you can be sure I’ll let you all know here in my blog what I think, I can already think of a huge number of scenarios where this is really going to help.

Suffice to say though, I’m really looking forward to once again producing real clean code that’s easy to read and maintain, imagine how much less clutter your going to have in things like your Data Access layers without all the boiler plate setup code getting in your way.

We also have the “Post Sharp” creators presenting a segment on Lidnug next week demonstrating the system and talking about how it can be used with great effectiveness.

For now though, I have to go and get ready for tonights Lidnug presentation on “Object Storage in WP7 Silverlight Applications by Jeremy Likness” which as always can be attended by installing Microsoft Live Meeting and Pointing your browser here

Have fun, I’ll see you all soon.

Shawty

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s