views:

235

answers:

1

Consider this about the up-coming Iron Python implementation.

In theory it would allow monkey patching right? Good or bad, it matters not. Given someone else's non-testable code (at least easily) the dynamic keyword would allow monkey patching would it not? This has great potential for C# and VB if I'm reading it right.

What are the thoughts on this?

+1  A: 

The dynamic type support in .Net 4 does not really give you full monkey-patching support across the CLR type system. It give you another way to mock objects, but to be frank the current way is plenty good (see moq: http://code.google.com/p/moq/)

With dynamic types, you lose intellisense, so an argument could be made that it is not advisable to dynamic for mocking.

The IronPython style interception will allow you to wrap up an existing object with your own desired behavior, but it will not allow you to tell the framework, to patch all Foo objects (from this point onwards) so Bar method will call Bar2 instead of Bar.

Keep in mind, with IronRuby and IronPython there are 2 separate type systems in play, there is the underlying CLR type system and the IronRuby/IronPython type system, when they call out to C# code there is marshalling going on. So even though IronRuby/IronPython can properly monkey patch their own type system, they can not use the same mechanism to patch the CLRs type system.

If you want monkey patching you need proper interception and that is hard: http://stackoverflow.com/questions/1331851/dynamic-interception-of-calls-in-net/1331869#1331869

Sam Saffron
But if the method names were the same, surely the IronPython 'mock' implementation would be used, so you could patch existing code? This is all hyperthetical, I've not actually used .NET 4.0 yet.
Finglas
If you have a container (factory) you use for instantiation and use it everywhere to get new instances AND wire it to apply the interception then yes. But it would require a lot of work. plus with dynamic you lose intellisense and can not catch trivial typos at compile time
Sam Saffron