How to return an IEntityCollection2 from a string?

Posts   
 
    
kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 28-Mar-2013 09:58:24   

I have this legacy function that gets a string and need to return an IEntityCollection2.

It uses reflection but I would like to be able to instantiate the classes directly. This is the current function:


 Public Function CreateEntityCollection(ByVal entityName As String) As IEntityCollection2
        Dim dynamicAssembly = Assembly.LoadFrom(DALFileName)
        Dim assemblyName = Split(dynamicAssembly.FullName, ",")(0)
        Dim sEntityName = assemblyName & ".EntityClasses." & entityName

        Dim type = dynamicAssembly.GetType(assemblyName & ".HelperClasses.EntityCollection`1[" & sEntityName & "]", True)   'Get the generic collection type

        Dim instance = Activator.CreateInstance(type)   'Call the constuctor to create the entity collection

        Return instance

    End Function

Ideally I would be able to write something like this


Public Function CreateEntityCollection(ByVal entityName As String) As IEntityCollection2
        Return New EntityCollection(Of CreateEntity(entityName))
End Function

Where CreateEntity is


Public Function CreateEntity(ByVal entityName As String) As EntityBase2
        Return FactoryClasses.GeneralEntityFactory.Create( _
            CType(System.Enum.Parse(GetType(EntityType), entityName), EntityType))
    End Function

But that obviously doesn't work. I'm new to LLBLGen, is there a factory I can use to generate this?

Thanks

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 28-Mar-2013 10:37:23   

I tried to use


Dim myEntityFactory2 As IEntityFactory2 = New EntityFactoryBase2(entityName, CType(System.Enum.Parse(GetType(EntityType), entityName), EntityType))

Dim myEntityCollection2 As IEntityCollection2 = New EntityCollection(myEntityFactory2)

But that returns


{myAssembly.HelperClasses.EntityCollection}

I need


{myAssembly.HelperClasses.EntityCollection(Of myAssembly.EntityClasses.myEntity)}

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 28-Mar-2013 16:17:23   

Found the answer for it in another thread (https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=17964)

and updated my code to


Public Function CreateEntityCollection(ByVal entityName As String) As IEntityCollection2
        Dim myEntity = CreateEntity(entityName)
        Dim entityfactory = myEntity.GetEntityFactory()
        Dim collection = entityfactory.CreateEntityCollection()

        Return collection

End Function