Search This Blog

Friday, March 12, 2010

This article is based on Vipul Modi's DynamicProxyFactory , i have made a little modification to the implementation in the code,

If you download & view the sample from here site you'll find out that the method InvokeComplexCalculator has an object of DynamicComplexNumber class created inside.

Consider a scenario where the class object is being passed as a parameter to the InvokeComplexCalculater function and the wsdl url & the service contracts are also dynamic i.e we call different services dynamically which have different service contracts also we have different class objects.

DynamicProxyClass responseProxy = new DynamicProxyClass(someResponse);
// Get the Type of the Proxy Response Class so that we get all the properties
// & their values dynamically
Type responseProxyType = responseProxy.ObjectInstance.GetType();

// Get the type of the response object which we would be sending back
// This is done to populate the values into the response object
Type responseType = response.GetType();

// Set the values from the Dynamic Reponse Class to the Response Class which
// we want to send back as a return value
foreach (PropertyInfo propertyInfo in responseProxyType.GetProperties())
{
// Skip the extra properties which are generated dynamically by the Dynamic Proxy Object
if (responseType.GetProperty(propertyInfo.Name) != null)
{
responseType.GetProperty(propertyInfo.Name).SetValue(response,
propertyInfo.GetValue(responseProxy.ObjectInstance, null), null);
}
}
.............
}

and below we have our request & response classes defined the normal WCF way

1 comments:

Cool one. The only trouble I see is the memory trouble associated with dynamic objects. MS itself says Reflection can be a overhead in normal practices. However, the possibilities of this is awesome. Also, I will love this, if it can be modified by using generics rather than hardcoding assembly names(someway or the other, assembly names/types are getting hardcoded here), which in turn makes it more type safe.