Dynamic fetching of an EntityCollection

Posts   
 
    
bchebrou
User
Posts: 18
Joined: 14-Feb-2006
# Posted on: 07-Feb-2007 17:52:24   

Hello,

Is it possible to get the EntityType (Public Enum EntityType As Integer) of an Entity at runtime ?

My goal is to expose a service (remoting) which make it possible to the users to get an EntityCollection and all related entities, only with the type of the first entity.

public IEntityCollection2 GetEntityCollection(Type entityType)
{
    // Create an EntityCollection with the IEntityFactory2 of the entity :
    // Get with reflection and CreateEntityFactory()...

    // Create a prefetchPath : here i need to get the value of the EntityType

    // Add dynamically PrefetchPathElement (reflection ?)

   // Fetch and return the Collection

}

I need to do this because i've a lot of settings table. I think is not to hard to generate at runtime controls in a winform to modify or create entity. But before i need to find a generic way to get all the data.

Do you think, this is possible ?

Cheers,

Benjamin

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 07-Feb-2007 18:31:57   

bchebrou wrote:

Hello,

Is it possible to get the EntityType (Public Enum EntityType As Integer) of an Entity at runtime ?

I didn't undertand what you want to get... do you want an instance of a Type object representing the EntityType enumeration? You can get this object having a reference to the assembly containing this type.

bchebrou wrote:

My goal is to expose a service (remoting) which make it possible to the users to get an EntityCollection and all related entities, only with the type of the first entity.

public IEntityCollection2 GetEntityCollection(Type entityType)
{
    // Create an EntityCollection with the IEntityFactory2 of the entity :
    // Get with reflection and CreateEntityFactory()...

    // Create a prefetchPath : here i need to get the value of the EntityType

    // Add dynamically PrefetchPathElement (reflection ?)

   // Fetch and return the Collection

}

I need to do this because i've a lot of settings table. I think is not to hard to generate at runtime controls in a winform to modify or create entity. But before i need to find a generic way to get all the data.

Do you think, this is possible ?

Cheers,

Benjamin

To create at runtime a typed entitycollection you can do the followyng


dim pEntityType As Type
Dim genericType As Type
genericType = GetType(EntityCollection(Of )) 'Get a Type representing the generic EntityCollection

Dim boundType As Type
boundType = genericType.MakeGenericType(pEntityType) 'Create the Type representing the typed EntityCollection(Of pEntityType)

dim myEntityColl as IEntityCollection2
myEntityColl = system.Activator.CreateInstance(boundType)  'get an instance of the typedEntityCollection

This is VB, but I hope you get the idea... simple_smile

bchebrou
User
Posts: 18
Joined: 14-Feb-2006
# Posted on: 08-Feb-2007 09:42:21   

Ok, thanks for the code, it's a better way that using the reflection to call CreateEntityFactory simple_smile

Max wrote:

bchebrou wrote:

Hello,

Is it possible to get the EntityType (Public Enum EntityType As Integer) of an Entity at runtime ?

I didn't undertand what you want to get... do you want an instance of a Type object representing the EntityType enumeration? You can get this object having a reference to the assembly containing this type.

Here my goal is to get the value of the EntityType Enum for an entity at the runtime. Because I want to write something like this (in VB this time wink )

Dim entity as IEntity2 ' Or EntityBase2
Dim myEntityCollection as IEntityCollection2 ' Use your code to create the correct typed instance


Using adapter As New DataAccessAdapter()

    ' Don't know if it's possible ?
    Dim entityTypeValue as Integer = entity.GetEntityTypeValue()

    Dim prefetchPath As IPrefetchPath2 = New PrefetchPath2(entityTypeValue)

    ' Complete prefecthPath using reflection ?

    adapter.FetchEntityCollection(myEntityCollection, Nothing, prefetchPath)

End Using

So, my problem is to write "entity.GetEntityTypeValue()", is it a way to do it ?

Regards,

Benjamin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 08-Feb-2007 10:05:34   

That's already available. LLBLGenProEntityTypeValue property is your friend simple_smile

Frans Bouma | Lead developer LLBLGen Pro
bchebrou
User
Posts: 18
Joined: 14-Feb-2006
# Posted on: 08-Feb-2007 10:14:31   

Yes thanks ! Sorry, Didn't find it by myself... Intellisense hides my friend stuck_out_tongue_winking_eye