Tuesday, March 31, 2009

AOP in .NET

So, despite promising a treatise on REST in WCF/.NET, I realize that others have said it far better than I (e.g.):

Jon Flanders
Ryan Tomayko (i liked this a lot, despite the stupid complaints of sexism. sheesh.)
and, oh, this company called Microsoft.

So, on to another pet project: aspect-oriented programming.

I've been obsessed with writing really lean, tight code as of late. In looking back on some of my work in the last couple years, having a new perspective of (the lack of) ROI on some premature optomization, I can pick out dozens of places where I would have thought (then) "oh, some day someone will need this" whereas I'd say now "is anyone honestly going to use this?" Of course, following TDD as a methodology helps me there too ;-)

So, I want to log exceptions. I also, for debug purposes, might want to log, well, everything.

I have it narrowed down to two options: runtime weaving with Spring.NET and compile-time weaving with PostSharp. The terms are self-explanatory (unlike other AOP jargon). While I think either could be used in either case, they have clear strengths in their respective foci (or, focuses).

Spring.Net. It's all runtime, meaning it's .NET language agnostic, and it all plays very, very nicely with Spring's IoC containers. I noted it doesn't allow you to do field interception (cool idea, but the power is too ripe for abuse and leads to bad OO design), just method interception, but that lets you cover everything you honestly need to do . . . Still I hate it when I know I can't do something, even though I have no business needing to do it.

PostSharp, as I mentioned, does do runtime weaving, but it really does an amazing job with compile-time weaving. If you've dealt with this technique, your big objection is noted. Yes, it's dificult to debug and, yes, it is magic. I know, it scares me too. But, when, (as in the case of log4PostSharp) I can literally have system-wide logging with ONE line of code in AssemblyInfo, I'm hooked. It's a fair trade.

I'll see how it goes. Though, I've built fairly robust logging into three separate service stacks in the course of a few hours using PostSharp.

I think I'm hooked.

No comments: