8

Possible Duplicate:
EF4 Cast DynamicProxies to underlying object

I'm trying to figure out how to clone or convert a System.Data.Entity.DynamicProxies into it's actual class. Eg:

System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class

All of the classes in MyApp.Entities inherit from BaseEntity, so I tried to do the converting there:

public abstract partial class BaseEntity
{
    public T ShallowCopy<T>() where T : BaseEntity
    {
        return this.MemberwiseClone() as T;
    }
    // other BaseEntity properties not relevent here
}

And then converting the DynamicProxies into the real class:

// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency(); 
// this one needs to return a Entities.Currency class 
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;

The reason for this is that I want to remove all Entity Framework tracking and etc from this object and just store its plain (POCO) properties in the cache. And I will need to be able to do this for all 100 or so Entity classes, so it has to be reasonably generic - without manually saying object1.foo = object2.foo for every single property.

Community
  • 1
  • 1
JK.
  • 21,477
  • 35
  • 135
  • 214
  • Why you didn't turn off proxy generation as you asked in previous question? – Ladislav Mrnka Jul 14 '11 at 10:51
  • Turns out that is not what I needed. I do need them to be proxies (with all the tracking etc) for most of the time. But when caching for later read only use it needs to be a non proxy object see http://stackoverflow.com/questions/6688772/c-entity-framework-using-only-one-objectcontext-per-httpcontext – JK. Jul 14 '11 at 10:53
  • You can turn off proxy creation selectively for some context instances or even queries so Imho you should be fine with turning off proxies only for loading your currencies. – Ladislav Mrnka Jul 14 '11 at 11:14
  • I'm using a repository - service pattern, so the actual objectcontext is not accessible, it is two layers separated from the code that calls _service.Get() which calls repository.Get() which calls objectcontext.asQueryable().Where(..etc..) – JK. Jul 14 '11 at 11:24
  • In such case you need special method like `GetUnproxied()`. Architecture should not demand how would you retrieve data. If you need special method simply expose it. Generic approach is fairy tale. – Ladislav Mrnka Jul 14 '11 at 11:30

1 Answers1

1

Maybe this article is helpful. It discusses several methods for cloning data. I'm not sure if these methods can be used to convert an object of type A to an object of type B. But it's definitely worth a try.

I would be very interested in the outcome of this, since this NuGet package also uses the generic repository pattern and memcached to address the same caching technique and your problem seems to be same there when deserializing data.

erikvda
  • 38
  • 5
  • 1
    @JK Could you provide which method made it work for you, I'm very interested, since I have the same issue in my project. – erikvda Aug 17 '11 at 07:52