views:

2223

answers:

3

I am looking at adding logic to a library I am working on that would require the need for a Dynamic Proxy. I would like to get some advice from user's who have used these two library's in a production environment. Does one out perform the other, were there any shortcoming's which made you have to switch to the other, etc. Basically tell me your experiences with the library's. The answers will help me decide which one to use.

-- Edit --


I forgot to mention that the library I am developing will support Mono, therefore any knowledge you can share about the two libraries and their support for Mono would be great also.

+3  A: 

Linfu is a more lightweight proxy generator than the Castle proxy generator.

When deciding which to use, to be honest it doesn't make much difference.

According to the author, Linfu will greatly outperforms the Castle generator, but in my own observations of real world usage the difference in speed is marginal.

Having said that Linfu will outperform Castle, and I'm not aware of anything Castle has over it, and so I always use Linfu.

Cocowalla
What do you mean by 'lightweight'?
Krzysztof Koźmic
By lightweight I mean less code, smaller assembly and (OK, arguably depending on how you use it) faster.
Cocowalla
@Cocowalla - Are you using the latest version that is based on Mono.Cecil. Also, can you share experiences running LinFu in Mono?
Dale Ragan
Sorry, I'm using Linfu DynamicProxy 1.0.3 with NHibernate, in Windows :) I may be incorrect, but I don't think that the new version works with NHibernate yet.In my usage I see around a 1% performance benefit using Linfu over Castle. It's not much, but since I'm not using Castle any where else in my app I've no reason not to use Linfu (if I was using Castle elsewhere I'd stick with that just to keep dependencies down)
Cocowalla
+6  A: 

I am a committer to Castle, contributing to Dynamic Proxy, so I may be biased, but I generally think Castle's Dynamic proxy is far better solution. I'm talking here about LinFu DynamicProxy v1.0 because that's what I'm familiar with. LinFu.Proxy 2 is based on Mono.Cecil and is rewritten from the scratch.

  • Castle covers wider range of scenarios.
  • Castle has (a lot) larger user base, and is proven in many OSS and commercial applications
  • Castle is actually performing better (link)
  • Castle has cleaner, easier to use API for example invoking target method to Castle DynamicProxy looks like this:

invocation.Proceed();

for LinFu, it looks like this (actual method/property name may vary as I'm writing it from memory)

//invocation.TargetMethod is MethodInfo, so you're using reflection
invocation.TargetMethod.Invoke(invocation.Target,invocation.Parameters);
  • Castle has an active user group where you can quickly get answers to your questions.

The performance issue, mentioned by the other answer are not DynamicProxy issue, but are result of bug in Microsoft's implementation of BCL (on Mono there is no such issue BTW). This only manifests itself when you have many (over 200+) proxy types in single ModuleScope.

Solution is trivial - don't generate that many proxy types (usually you won't have to) or use many ModuleScopes/ProxyGenerators (for example Rhino.Mocks uses this approach)

Personally I do not develop on Mono, so I don't have firsthand experience, however there are libraries using Castle DP on Mono, and we didn't get any compliaints so i guess it works just fine.

Since my benchmark few months ago, there has been no new release of Castle DP (new version is targeted at the end of the year). LiFu has a 2.0 version, but I'm not sure if it is trunk only, or released. I don't know about Spring or Unity.

Krzysztof Koźmic
@Krzysztof - Thanks for the link on the performance tests you have run. I see the tests were about 6 months ago. Have you tried running the tests again with the latest versions of each framework? I am not sure if there have been new versions since then. Also, I forgot one thing in my question and I edited it. Could you edit your answer to include any experience's testing under Mono?
Dale Ragan
I have ran tests of Dynamic Proxy (trunk) against version 2.1. While the interception times have not changed (and it works **very** fast) the proxy type generation is now multiple times faster
Krzysztof Koźmic
A: 

We had some perf issues related to LinFu vs Castle in 2.0.1. http://niemware.blogspot.com/2009/11/nhibernate-21-performance-issues-with.html

Trent