C# to Ruby - Proxies
I am learning Ruby, and one why I am doing so is with the Ruby Koans. Tonight I completed the Koan: “About Proxy Object Project.”
What is a proxy and why would you want to implement one? In its simplest form, a proxy is an interface or middleman to something else this could be anything: the file-system, the network or any other resource. A common use of a proxy is for intercepting calls and logging them. You can read more on Wikipedia.
You need the class you want to proxy and the proxy class implementation that will intercept the calls do something and then invoke the desired method call. I would like to compare the C# implementation and the Ruby implementation.
Let’s start with the C# implementation. I got this from StackOverflow:
Let us examine this code to understand what is going on. The first thing we see is we need to add a reference and using statement to System.Runtime.Remoting.Proxies.RealProxy in order to inherit from RealProxy. The LoggingProxy is generic which will give us type safety. The class is a Singleton since it has a private constructor and needs to be created using the Create factory method. Finally, there is the Invoke method that will get called whenever a method is called on our proxied class that will do some logging for us and has some exception handling to ensure we don’t fall over ourselves.
Now to use the proxy class all you would need to do is:
Pretty simple, right? Nothing spectacular, there is some casting going on to ensure we get our method, but in terms of complexity not quite hectic? Well maybe…are you ever going to remember this by heart?
Let’s contrast that with how the Rubyists do this sort of thing
Again let’s walk through this code. First thing is that Ruby is a dynamic language and therefore we don’t have a generic class that will ‘Strongly’ type our proxied class. We have initialize that takes an instance of our object that will be proxied, so there is no Create factory method. Also, no private constructor so this is not a singleton. We then extend the method_missing method do our logging in a begin rescue block and proxy off to our class the method that was called together with any args or blocks.
Now how would you use this?
Wow, that is a quite a bit of typing. I much prefer the Ruby approach which saves a lot of keystrokes. I am not going to declare that one is better or superior to the other. Ruby and C# are two different languages with different strengths and weaknesses. But damn I know which I would rather want to use.
In conclusion, its pretty clear that C# takes a long winded approach to creating proxies, and this is not very obvious to implement. The Ruby approach whilst very concise doesn’t give you the security of type safety. Personally I would prefer to write the Ruby code as it is a lot more straightforward and much easier on the eyes.
I am going to try and do these comparisons regularly. If anything its a way for me to learn more about both languages