Introducing the Osherove.Interception framework

Following the Nunit Extensibility framework, I did some thinking and made a More generic framework for interception ability. That means that you can create your own interception attributes not only for unit testing but for real world applications as well.
the new solution consists of more real world examples (well, one, but a cool one).
The coolest example so far is an idea triggered by Peli - it's a Caching attribute.
In this case I created an object that has properties and methods that have an [AutoCache] attribute on them.
this makes them automatically return values from a simple cache object.
Actually - the attribute takes care of everything so the properties and methods actually return NULL.
Yep - it magically just works.
Give it a shot. I like this idea and I think it might have a future.
what do you think?
 
Download:
 
 
Example:
public class MyDataObject:CachedPropertyBag
{
      [AutoCached("CustomerList")]
      public string[] GetCustomerList()
      {
         //the beauty here is that any callers to this method will actually
         //get a string array with 3 items in it.
         //can you say "automagically", boys and girls?
            return null;
      }
}
Published Tuesday, October 12, 2004 6:17 AM by RoyOsherove
Filed under:

Comments

Tuesday, October 12, 2004 10:43 AM by Jack

# re: Introducing the Osherove.Interception framework

Hmmmm...

I can't say automagically but I can say boy this is confusing non intuitive code. Attributes in general make it hard to write code that is maintainable especially when attributes collide, or you remove on attribute and forget to remove another.

Attributes seem to muddy the water and when you have an attribute that is going to change the return value of a method I think this would lead to more confusion than the cool factor it is worth.
Tuesday, October 12, 2004 10:44 AM by Jack

# re: Introducing the Osherove.Interception framework

PS... Here is a web log that got me thinking about my use of attributes

http://weblogs.asp.net/stevencl/archive/2004/10/08/239833.aspx
Tuesday, October 12, 2004 12:56 PM by Roy Osherove

# re: Introducing the Osherove.Interception framework

Jack - yes - the code is not intuitive.
I didn't say this was production ready - just that i had a cool idea and wanted to try it.
Obviously caching can be implemented with these attributes and _still_ keep the code readable.
Personally I like attributes and think they do contribute to readability - if acted upon correctly. the sample here is an extreme one and as such is just meant to make a point - you _could_ do this if you wanted to. now go and think of the possibilities to do this in a user/developer friendly manner.
Tuesday, October 12, 2004 3:39 PM by Kirk Marple

# re: Introducing the Osherove.Interception framework

hey Roy,

have you looked at the performance impact of the ContextBoundObject?

i'd heard rumors that ContextBoundObject was slow, but haven't tried it in practice.

i've looked at this approach vs. a post-compile step like XC# for injecting code via attributes. i'd like to have automated parameter validation via attributes, and it seems like one of these two approaches will work, but there are tradeoffs.

thx,
Kirk
Tuesday, October 12, 2004 5:53 PM by Roy Osherove

# re: Introducing the Osherove.Interception framework

Kirk - yes. It's somewhat costly, no doubt. That's why its very suited for Testing - which is usually somethign you expect can take a little more time.
FOr real world stuff I think it might be suited for various small corners which can allow less performance with better applicable benefit.
I'm still working out some ideas though.
Tuesday, October 12, 2004 5:59 PM by Kelly Summerlin

# re: Introducing the Osherove.Interception framework

I had cooked up something for Remoting that was similar to this using attributes and ContextBound objects. But because early on things about ContextBound were wrapped in the this is a very obscure corner of .NET and might go away in future versions enigma, I left it alone. ContextBound is very cool.
Tuesday, October 12, 2004 8:21 PM by TrackBack

# Interception Framework?

Wednesday, October 13, 2004 2:55 AM by TrackBack

# Roy does interception . . .

Wednesday, October 13, 2004 5:50 AM by JosephCooney

# re: Introducing the Osherove.Interception framework

Hi Roy - although the example is a little confusing I think attributes in general are a cool way of seperating out concerns, and being more declarative in code (altho Isee from the comments they are not everybody's cup of tea). I tried to do something similar recently http://dotnetjunkies.com/WebLog/josephcooney/archive/2004/09/27/26893.aspx but couldn't get it to work on anything except context bound objects. I was sort of hoping you had figured out a way around this but I guess not.
Wednesday, October 13, 2004 7:23 PM by Addy Santo

# re: Introducing the Osherove.Interception framework


The problem with automatical solutions is that they usually don't know your intent and require explicit "hints" on how to act. for example, in the caching scenario you demonstrated- how/when are items invalidated? Since the caching is done orthogonally to the actual business scenario, the caching logic has no way of knowing or deciding what is a reasonable lifetime or invalidation criteria. And so you wind up adding more and more meta-data to the attribute. This then requires a delicate balancing game to ensure that the simplicity gained by abstracting the logic into attributes isn't lost due to the complexity of typing together two disparate systems - the application and the attribute-based framework.

I need a vacation.
Thursday, October 14, 2004 7:54 PM by Sam

# re: Introducing the Osherove.Interception framework

Addy: I don't see why you couldn't expose the same sort of properties the HttpContext Cache does. That seems flexible enough. Even a very basic cache like:
<code>
public enum Expiration() { Absolute, Sliding }

AutoCache(Expiration expiration, long ms)
</code>
Can be very useful for quick/lightweight stuff, and realistically, it's probably as far as most developers ever go with caching for better or worse.

I'm personally more interested in validation attributes though...

<code>
// Property in User class:
[SqlBound(SqlDbType.Varchar, 30)]
[RegexValidator("[A-Z0-9_]{6,}")]
public string LoginName
{
get { return this._loginName; }
set { this._loginName = value; }
}
</code>
Friday, October 15, 2004 11:41 AM by Paul D. Murphy

# re: Introducing the Osherove.Interception framework

Actually I wrote an aspect driven caching system years ago and posted it to the learnasp.com lists. I revived the code on my blog about 8 months ago. I also wrote an aspect driven database adapter. The problem is that no one at the time seemed to care, and I gave up on it considering the coupling it creates.

The end result of my study was a conversation with a indigo team member. He told me that indigo would provide a 'real' AOP solution built around attributes called 'behaviours'.

Paul

Friday, October 15, 2004 11:43 AM by Paul D. Murphy

# re: Introducing the Osherove.Interception framework

Tuesday, October 19, 2004 9:51 AM by TrackBack

# Getting FIT, testing GUIs, counting on unit tests...

Sunday, January 23, 2005 2:29 AM by TrackBack

# New stuff on Team Agile: XtUnit and Interception block with basic documentation

Monday, January 24, 2005 2:54 AM by TrackBack

# RE: New stuff on Team Agile: XtUnit and Interception block with basic documentation

Monday, January 24, 2005 7:24 AM by TrackBack

# Need some extra Cache?

Monday, January 24, 2005 7:26 AM by TrackBack

# Need some extra Cache?

Tuesday, January 25, 2005 3:29 AM by TrackBack

# Need some extra Cache?

Wednesday, June 21, 2006 4:14 PM by ISerializable - Roy Osherove's Blog

# New stuff on Team Agile: XtUnit and Interception block with basic documentation

Update: Fixed broken links (21-6-06)
I just finished adding a new downloads section to the Team Agile...
Tuesday, July 04, 2006 3:10 AM by Uri Dor

# re: Introducing the Osherove.Interception framework

Hi, Roy, the 'download here' link on this page is still broken: http://www.osherove.com/LinkClick.aspx?link=http%3a%2f%2fwww.osherove.com%2fdownloads%2fosherove.interception.zip&mid=347 but I guess it's the same as what I found on team agile: http://teamagile.com/downloads/TeamAgile.ApplicationBlocks.Interception.zip which does work.