Dynamicly create EntityCollection

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 06-Jun-2007 14:55:03   

Hi,

I use the following to create a entity from a string containing it's name:


            EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
            IEntity entityInstance = GeneralEntityFactory.Create(typeOfEntity);
            return entityInstance;

Is there something similair for collections?

Thanks, Gab

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2007 15:47:08   

Starting from where you have stopped, using the entity instance:

IEntityCollection entColl = entityInstance.CreateEntityFactory().CreateEntityCollection();
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 06-Jun-2007 20:05:19   

Hi,

Problem is that I can't find "CreateEntityFactory" on the EntityInstance

What do I do wrong? Selfservicing?

Cheers, Gab

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 07-Jun-2007 09:37:40   

For Adapter, I use this code:

Public Shared Function CreateNewTypedEntityCollection(ByVal DbGenericAssemblyReference As System.Reflection.Assembly, ByVal EntityName As String) As IEntityCollection2
        'get entity full name
        Dim fullEntityTypeName As String
        fullEntityTypeName = DbGenericAssemblyReference.FullName.Substring(0, DbGenericAssemblyReference.FullName.IndexOf(","c)) & ".EntityClasses." & EntityName


        'get entity Type
        Dim entityType As Type
        entityType = DbGenericAssemblyReference.GetType(fullEntityTypeName, False, True)



        'get name of GENERIC entity collection
        Dim UNboundGenericEntityCollectionTypeName As String
        UNboundGenericEntityCollectionTypeName = DbGenericAssemblyReference.FullName.Substring(0, DbGenericAssemblyReference.FullName.IndexOf(","c)) & ".HelperClasses.EntityCollection`1"


        'get UNBOUND type of GENERIC entity collection
        Dim UNboundGenericEntityCollectionType As Type
        UNboundGenericEntityCollectionType = DbGenericAssemblyReference.GetType(UNboundGenericEntityCollectionTypeName, True, True)

        'create typed (BOUND) type of the entityCollection
        Dim BOUNDgenericEntityCollectionType As Type
        BOUNDgenericEntityCollectionType = UNboundGenericEntityCollectionType.MakeGenericType(entityType)

        'create the instance in the typed EntityCollection
        Dim ec As IEntityCollection2
        ec = DirectCast(System.Activator.CreateInstance(BOUNDgenericEntityCollectionType), IEntityCollection2)

        Return ec
End Function

this will give you a new instance of a typed entity collection. The entity collection will be "typed" on the entity specified.

The simplest way to obtain a reference to the DbGeneric assembly is to put somewhere in a class of the DB generic assembly the following code

Public Shared Function GetAssemblyRef() As System.Reflection.Assembly
        Return System.Reflection.Assembly.GetExecutingAssembly
End Function

If this code is in the DB Generic assembly, it will return a reference to the current instance of the DB Generic assembly.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Jun-2007 09:56:35   

Problem is that I can't find "CreateEntityFactory" on the EntityInstance

It's generated as protected.

        protected override IEntityFactory2 CreateEntityFactory()
        {
            return new OrdersEntityFactory();
        }

So maybe you want to change that or add a public wrapper method.