Proxy Generation

Dec 17, 2010 at 11:06 AM

Hello again,

When using Unity with policy injection, a proxy is generated and returned instead of the actual registered classes. This process is dynamic and happens, I believe, only the first time the object is requested.

Is it possible to pre-generate proxies (in external assemblies, of course) for our registered classes and use them by adding a reference to their assemblies? How difficult would it be to achieve this? I assume the proxy assemblies are generated in memory, perhaps it would be sufficient to save them to disk, no?



Dec 17, 2010 at 11:06 PM

It's possible, but not without changes to the code - we didn't build the code with this in mind. Creating the assembly is actually pretty easy; there's a debug hook in there actually (have to flip a compile switch) to write out the generated assemblies anyway for debugging purposes. It's basically the only way to know if the generated IL is actually correct or not - write it to disk, open it in Reflector and see where the errors are. :-)

So, net result - interesting idea, technically possible, not supported by the code base right now. I'd also want to do some profiling on the actual cost of proxy class generation to see what you'd actually get back time-wise.



Dec 17, 2010 at 11:53 PM

Hello, Chris!

I got this idea from NHibernate and related project NHibernate ProxiyGenerators, I also think it's interesting.

Can you please give me some directions, for example, what is this switch?



Dec 19, 2010 at 9:28 PM

You might want to consider using a T4 template to generate the interception proxies.

I do this to generate WCF proxies based on the service interfaces.

If you're not familiar with the T4 system, Scott Hanselman has a decent introductory blog post, and quite a few links from there