Need more type specificity when creating EntityGraph (need EntityGraph<TEntity> not EntityGraph<Entity>)

Dec 18, 2011 at 11:37 PM

(Sorry for the double post, but I realized that the original post over in the EntityGraph-specific project has very little traffic - this appears to be the much better place)

I love the idea of the EntityGraph stuff in this project and it seems like it will do exactly what need... except in one specific case.

I'm using:

  • Silverlight 4
  • Caliburn.Micro
  • EF 4.1 with Code-First
  • EntityGraph (via Nuget package)

Quick version of what I see as the problem with the EntityGraph code: calling the EntityGraph extension method (defined in Proxies.cs) to create my graph gives me back an instance of EntityGraph, but what I need is an instance of EntityGraph<TEntity>. I believe it's more accurate to say that it gives me an instance of EntityGraph<Entity>, but my point remains - I need it to be EntityGraph<TEntity> (tee-entity, not entity). Or, to put it even more differently, EntityGraph<MyConcreteEntityType> and not EntityGraph<Entity>.

Here's why. I'm using Caliburn.Micro because (in addition to many other things) it eliminates nearly 100% of the binding expression code that I would otherwise have to write. It uses controls' x:Name attributes to automatically set up the necessary binding expressions. Again, in most cases I have exactly ZERO databinding code in my xaml files.

Pretty much everything I'm trying to bind in my UI starts with an instance of an EntityGraph object whose Source property is an instance of my root entity. The problem is that when Caliburn looks at the Source property of my EntityGraph object, it sees that it is of type Entity, not "MyEntityTypeName". It uses reflection at runtime to make decisions about whether or not to apply my binding conventions, but because the root Entity from Ria Services doesn't have any of my entity's properties defined on it, it elects to not create the bindings.

I've tried a couple of different approaches to working around this. First, before thinking things through, I tried to do this:


var foo = myEntity.EntityGraph(myShape) 
     as EntityGraph<MyEntityType>;

But that fails to compile because there's no way to coerce something that's EntityGraph<Entity> into EntityGraph<MyEntityType>.


Then I tried inheriting from EntityGraphFactory and altering its Get method to return EntityGraph<TEntity>. This appeared impossible almost immediately, mainly because things like the factory instance were private.

Then I downloaded the code from this project and started hacking away. My thinking was that there would probably be no harm in just making it ALWAYS return the "very type-specific" version of EntityGraph (especially for my case), so I thought that it would be pretty easy. However, the changes I needed to make keep spiraling out to more and more of the codebase, so I'm thinking that maybe I'm just looking at this the wrong way.

Is anyone aware of a way to get a graph with a source property of MY entity type rather than the RIA Services' base Entity type?

Dec 22, 2011 at 7:39 AM

Your observations are correct: the TEntity type parameter of EnttiyGraph is used to instantiate the generic entity graph framework and not for specifying the source entity type.  This is pretty fundamental and not easy (or at all possible) to change. Your wish would require an additional type parameter just for recording the type of the source entity. Since this is the entity that you use to instantiate an entity graph you already know its type. I therefore do not see much gain in adding this type as an additional type parameter.

But, why do you bind to the entity graph and not to the source entity directly? If you're using a view model it is easy to expose the entity graph as a property of your view model as well as the source entity. You can then bind to either of them depending on your needs.