Google-Apps
Hauptmenü

Post a Comment On: the urban canuk, eh

"Creating a better Wrapper using AOP"

10 Comments -

1 – 10 of 10
Anonymous Anonymous said...

My favorite part of your post is the Linq usage to look for custom attributes. Creative idea, excellent article.

10:31 PM

Blogger Nitride said...

Just a note, the line

if (attribute == null) return false;

is meaningless after using Singel(), since it will throw an exception if no matching attribute is found. To return null if nothing is found you have to use SingleOrDefault()

3:48 AM

Anonymous Anonymous said...

The Linq part can actually be shortened (and corrected) to .OfType<MappedFieldAttribute>().SingleOrDefault(mf => mf.TargetType == _targetType)
The original code will fail if there are additional attributes and the null comparison will never evaluate to true.

7:51 AM

Blogger bryan said...

Linq is definitely fun! I was going for readability using the Select/Where/Single syntax, but your comments are valid and optimization here is needed. I've updated the sample.

This bug fix provides more meaningful exception information, as we'll be returning NotSupportedException instead of a NullReferenceException. I've updated the exception to provide more meaningful detail as well.

10:18 AM

Blogger naraga said...

Check out also AutoMapper (http://www.codeplex.com/AutoMapper). Eventhough its purpose is not exactly the same as what you are tryng to solve but sometimes it is only about data objects and then it is perfectly acceptable to do it in "more disconnected" way. but anyway, honestly i dont like neither solution. it is true that mapping is almost always just a very stupid boring code but you have almost always exceptions. What if field in adapter interface must be translated into two fields in target object. And this is really very simple transformation. Moreover i like when compiler do as much checking as possible for me and you are giving up this feature.

11:17 AM

Blogger bryan said...

Thanks for the AutoMapper recommendation.

You're point is valid, there's no compile time checking here. Either way, I wouldn't blindly expect this to work without a few unit tests in place.

To your point about very simple mapping, or complex boundary conditions, this sample does not address those concerns. Though with some creative thinking, it's not hard to imagine forwarding complex calls to a customized interceptor.

3:56 PM

Blogger Unknown said...

Nice article. You could use IInterceptorSelector and use interceptor per method strategy to improve performance (not pay the cost of reflection for each call) and make the code cleaner.

9:18 AM

Blogger bryan said...

Krzysztof,

Thanks for the feedback. The IInterceptorSelector looks interesting -- a custom selector is associated to the ProxyGenerator as part of the ProxyGeneratorOptions -- which in turn helps the ProxyGenerator determine which methods should be intercepted up front?

I can see how this would cut down on reflecting the incoming method to determine if it should be intercepted, but the remaining reflection would still be required to map the incoming method to the 3rd party class.

I keep meaning to come back to this post and optimize for performance/memory, and the IInterceptorSelector gives me some good ideas.

Cheers.

10:51 AM

Blogger Unknown said...

Bryan,

well - no. IProxyGenerationHook does that.
ProxyGenerationHook statically, during proxy type creation process decides which methods should be overriden hence, which you want to allow to be intercepted.
IInterceptorSelector operates on proxy _instance_. For each instance before first call to the method it gets called to decide which interceptors should be used for that particular method.

So you can use the Hook to filter out methods you dont want to intercept, and then with the Selector put appropriate interceptors for each method you want to intercept.

I prefer interceptor-per-method approach, that is each interceptor works with only one method (or all methods in case of general use interceptors), so that interceptor can get straight to the point, without wasting time verifying it indeed is intercepting the call it is interested in.

4:57 AM

Blogger bryan said...

Right on, Krzysztof. I'm working on an update for this post and realized my mistake after visiting those links the second time through.

Disappointing though -- I didn't see a GenerateProxyWithoutTarget overload that accepts a ProxyGeneratorOptions with Generics support.

2:08 AM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot