Question

I was looking at using DynamicProxy classes, and I'm fairly new to this concept. Before I got too far down this road, I was wondering how well these classes work with IntelliSense and type safety?

I'm just afraid of using something like Castle DynamicProxy (or some other ones), and after setting everything up finding out that using my objects provides no IntelliSense or type safety. Can anyone shed any light on this?

Was it helpful?

Solution

I'm looking for a straight answer on how DynamicProxy classes are used, and whether or not they support intellisense, and if so... how?

Well, in explaining how DynamicProxy classes work, you'll have a clear understanding to why they are type safe, and how they are able to work with intellisense so nicely.


Firstly, let's understand what a DynamicProxy actually is. A proxy class is a class that handles member calls on behalf of another class. This is either done through inheritance (most common) or through composition. So, if you were to hand-write a proxy class, here is what it could look like:

public class Customer
{
    public virtual string Name { get; set; }
    // etc...
}

public class CustomerProxy : Customer
{
    public override string Name
    {
        get
        {
            // Do additional functionality...

            return base.Name;
        }
        set
        {
            // Do additional functionality...

            base.Name = value;
        }
    }
}

Two (2) key features play a crucial role in this working appropriately, inheritance and polymorphism. So, to use the Customer class seamlessly, a ProxyGenerator simply would create an instance of the CustomerProxy class, but return it as a type of Customer. It would basically be the same thing as doing Customer customer = new CustomerProxy();. The "dynamic" portion doesn't really have anything to do with the .NET dynamic keyword, but instead should read "Runtime", because it simply means that the proxy class is generated at runtime (while the application is running), instead of at compile-time. Oh, and in case you are wondering how it does this, it uses System.Reflection.Emit


That's the simple version of what a DynamicProxy is. Different frameworks offer different features when it comes to creating these proxy classes. For example, in Castle Windsor's DynamicProxy one could create Mixins and apply additional interfaces to these proxy classes -- that is, your generated proxy class could potentially look something like this: public class CustomerProxy : Customer, ISomeInterface { ... }, even though the Customer class itself did not implement the ISomeInterface. Here is a really good resource for Castle's DynamicProxy (http://kozmic.net/dynamic-proxy-tutorial/). It goes through the various features and use cases for those features.

OTHER TIPS

It is and type safe so intellisense should work just fine with it. see this example: DynamicProxy tutorial

you can see that they use generics for instanciating the proxy classes. It means that its fully typed so you've got nothing to worry about.

Proxies can be seen as call interceptors (depending of the kind of proxy implementation), so when you are writing your code is like you were working with a defined interface or class so you will get intelliSense.

Then, depending of the kind of proxy you implement "some" calls to the class/interface members will be intercepted.

[Edit] If you use the dynamic keyword, for example when implementing a dynamic proxy using a DynamicObject obviously you will not have intelliSense, but this caused by the nature of the dynamic keyword and not by the proxy itself. You can take a look at this link to see how to implement a Proxy using a DynamicObject

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top