2

OK, the situation is we have a class, PatientDto, and a DynamicProxy generated by Castle, PatientDtoProxy.

We're using this proxy in the Silverlight client, then want to send it back to the server via a WCF service call.

The WCF service Contract expects a PatientDto (ie not the proxy) and, as expected, blows up if you try to send anything else.

Essentially, we feel like we should be "casting" it back to a PatientDto to get things to work... but in reality, even if you cast the reference down to PatientDto, it doesn't change anything -- WCF still sees the object in memory as a PatientDtoProxy and blows up.

Obviously, doing a deep-copy into a new'ed up PatientDto is an option (and does work), but an unpleasant one. Any techniques we're just not thinking of?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Bobby
  • 1,666
  • 3
  • 16
  • 27
  • Is it an interface based proxy or a class-based one? If the latter, doesn't your proxy instance already inherit from PatientDto? – Kirk Woll Aug 05 '10 at 16:52
  • class-based, and yep you're right it does - WCF still complains. I assume it's using reflection on the object itself, and sees that it's really a 'PatientDtoProxy'.. even though we've casted the -reference' to the base type... that's the dilemma – Bobby Aug 05 '10 at 16:56
  • 1
    Yeah, tricky. Ideally, you would set the KnownTypes attribute on the base class to include the subclass. Obviously this is not possible with a dynamically created proxy. :( – Kirk Woll Aug 06 '10 at 01:08

2 Answers2

1

What about using AutoMapper and mapping your proxy to a real PatientDto object. Or just manually mapping it yourself.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • 1
    sounds like the fallback option I mentioned - to do a deep-copy from the Proxy to a new object in memory of the base type.. seems like a suboptimal solution doesn't it? Some kind in-place cast would be ideal.. just not sure if that's possible – Bobby Aug 05 '10 at 16:40
  • 1
    There's nothing about mapping that is ever optimal. – Chris Marisic Nov 16 '10 at 21:49
  • 1
    just getting back to this - looking back doing a deep copy is probably the only way to go, so you get a check, finally. thanks! – Bobby Apr 17 '11 at 15:32
-1

Just to add a more favourable alternative to mapping to a new object, you can just extract the underlying object.

I use a helper class to do this:

using Castle.DynamicProxy;

namespace Magna.Client.Common.Proxy
{
    public class ProxyDtoUtils
    {
        public static T GetUnderlying<T>(T proxy)
        {
            return ProxyUtil.IsProxy(proxy) ? (T)ProxyUtil.GetUnproxiedInstance(proxy) : proxy;
        }
    }
}
Martin
  • 3,014
  • 2
  • 14
  • 13