Create Entity using EntityType

Posts   
 
    
Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 05-Mar-2005 08:42:45   

I'm creating a factory class for creating my entity objects, where the param the user passes in is of EntityType. Is there a way to create an object based on the EntityType value or do I just need to use a select...case construct? No big deal though, just not sure if there is a backdoor into this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 05-Mar-2005 10:50:45   

There is a generalentity factory created for you in the entityfactories file, which accepts an EntityType and produces an empty entity for you simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 05-Mar-2005 17:02:18   

Otis wrote:

There is a generalentity factory created for you in the entityfactories file, which accepts an EntityType and produces an empty entity for you simple_smile

Awesome stuff! I am doing the same for the entity collections. I guess the best way to accomplish the same thing would be to first call the factory, grab the entity object, and pass the value from the EntityFactoryToUse property to the entity collections constructor?

Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 05-Mar-2005 22:55:50   

Otis wrote:

There is a generalentity factory created for you in the entityfactories file, which accepts an EntityType and produces an empty entity for you simple_smile

Hmm....i can't seem to find the file. Do you know what the name of the actuall class is? I'm using adapter.

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Mar-2005 12:01:37   

Kristian wrote:

Otis wrote:

There is a generalentity factory created for you in the entityfactories file, which accepts an EntityType and produces an empty entity for you simple_smile

Hmm....i can't seem to find the file. Do you know what the name of the actuall class is? I'm using adapter.

Thanks!

Ah, no in adapter there is no such factory, as in adapter you need the entity factories themselves often, so such a general factory wasn't that helpful I thought. You can add one yourself though:


<[If HasEntity]>
    ''' <summary>
    ''' Factory to create new, empty Entity objects based on the entity type specified. Uses 
    ''' entity specific factory objects
    ''' </summary>
    <Serializable()> _
    Public Class GeneralEntityFactory
        ''' <summary>
        ''' Creates a new, empty Entity object of the type specified
        ''' </summary>
        ''' <param name="entityTypeToCreate">The entity type to create.</param>
        ''' <returns>A new, empty Entity object.</returns>
        Public Shared Function Create(entityTypeToCreate As EntityType) As IEntity2 
            Dim factoryToUse As IEntityFactory2 = Nothing

            Select Case entityTypeToCreate
<[Foreach Entity CrLf]>             Case <[RootNamespace]>.EntityType.<[CurrentEntityName]>Entity
                    factoryToUse = New <[CurrentEntityName]>EntityFactory()<[NextForeach]>
            End Select
            
            Return factoryToUse.Create()
        End Function        
    End Class
<[EndIf]>

Frans Bouma | Lead developer LLBLGen Pro
Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 06-Mar-2005 14:05:05   

Otis wrote:

Kristian wrote:

Otis wrote:

There is a generalentity factory created for you in the entityfactories file, which accepts an EntityType and produces an empty entity for you simple_smile

Hmm....i can't seem to find the file. Do you know what the name of the actuall class is? I'm using adapter.

Thanks!

Ah, no in adapter there is no such factory, as in adapter you need the entity factories themselves often, so such a general factory wasn't that helpful I thought. You can add one yourself though:


<[If HasEntity]>
    ''' <summary>
    ''' Factory to create new, empty Entity objects based on the entity type specified. Uses 
    ''' entity specific factory objects
    ''' </summary>
    <Serializable()> _
    Public Class GeneralEntityFactory
        ''' <summary>
        ''' Creates a new, empty Entity object of the type specified
        ''' </summary>
        ''' <param name="entityTypeToCreate">The entity type to create.</param>
        ''' <returns>A new, empty Entity object.</returns>
        Public Shared Function Create(entityTypeToCreate As EntityType) As IEntity2 
            Dim factoryToUse As IEntityFactory2 = Nothing

            Select Case entityTypeToCreate
<[Foreach Entity CrLf]>             Case <[RootNamespace]>.EntityType.<[CurrentEntityName]>Entity
                    factoryToUse = New <[CurrentEntityName]>EntityFactory()<[NextForeach]>
            End Select
            
            Return factoryToUse.Create()
        End Function        
    End Class
<[EndIf]>

Great, thanks for the code! I am using the derived entity templates. Do I need to add anything special to the above code?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Mar-2005 15:59:12   

Yes, you have to prefix the entity factory names, so factoryToUse = New My<[CurrentEntityName]>EntityFactory()<[NextForeach]>

Frans Bouma | Lead developer LLBLGen Pro
Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 07-Mar-2005 16:01:54   

I am doing the same for the entity collections. Is there a way to know what factory is associated with a given entity so I can pass that to the EntityCollection's constructor?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Mar-2005 17:55:55   

Kristian wrote:

I am doing the same for the entity collections. Is there a way to know what factory is associated with a given entity so I can pass that to the EntityCollection's constructor?

That's not defined by definition in adapter, there is no default factory for an entity type. There is one generated but you can also add your own if you want.

Frans Bouma | Lead developer LLBLGen Pro
Kristian
User
Posts: 30
Joined: 24-Feb-2005
# Posted on: 07-Mar-2005 20:08:16   

Otis wrote:

Kristian wrote:

I am doing the same for the entity collections. Is there a way to know what factory is associated with a given entity so I can pass that to the EntityCollection's constructor?

That's not defined by definition in adapter, there is no default factory for an entity type. There is one generated but you can also add your own if you want.

Ok thanks. I just went ahead and created a GetEntityFactory() method off of the GeneralFactory class. It essentially does the same the the Create() method does, except it returns a factory.