GenericEntityCollection to EntityCollection and vice versa

Posts   
 
    
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 17-Aug-2007 18:10:49   

It is sometimes necessary to convert a generic entity collection to a general entityCollection and vice versa. The utility functions I wrote to this are as follows. I wanted to make sure this is the best way to do this:

to convert generic entityCollection to the general one

   Public Function GenericEntityCollectionAsEntityCollection(Of TEntity As EntityBase2)( _
         ByVal collectionToConvert As EntityCollection(Of TEntity)) As EntityCollection

      Dim toReturn As EntityCollection = Nothing

      If collectionToConvert IsNot Nothing Then
         toReturn = New EntityCollection(collectionToConvert.EntityFactoryToUse)
      End If

      For Each ent As TEntity In collectionToConvert
         toReturn.Add(ent)
      Next

      Return toReturn
   End Function

to convert a general entityCollection to the generic one

   Public Function EntityCollectionAsGenericEntityCollection(Of TEntity As EntityBase2)( _
         ByVal collectionToConvert As EntityCollection) As EntityCollection(Of TEntity)

      Dim toReturn As EntityCollection(Of TEntity) = Nothing

      If collectionToConvert IsNot Nothing Then
         toReturn = New EntityCollection(Of TEntity)(collectionToConvert.EntityFactoryToUse)
      End If

      For Each ent As TEntity In collectionToConvert
         toReturn.Add(ent)
      Next

      Return toReturn
   End Function

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Aug-2007 10:43:43   

For what do you need this exactly? If it's not webservices, you could of course access everything in the collection via IEntityCollection2, which is the non-generic interface to the generic collection wink

Frans Bouma | Lead developer LLBLGen Pro
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 18-Aug-2007 15:28:54   

Thats exactly the conclusion I reached to last night..

Generics are great but I guess following the known and proven pattern of programming to interfaces should never be ignored.