Possible to create an EntityCollection from EntityBase2?

Posts   
 
    
Hinkel
User
Posts: 4
Joined: 07-Feb-2005
# Posted on: 15-Nov-2006 21:41:21   

Using 2.0 from 11/7/06, Adapter.

We have a base service which each of our Entity services extends.

public abstract class BusinessService<TEntity> : IBusinessService<TEntity> where TEntity : EntityBase2, IEntity2

We have the following method:

         public EntityCollectionBase2<TEntity> FetchCollection()
        {
            //TODO: Create IEntityCollection2 from TEntity
            //
            //
            DataAccessAdapter.FetchEntityCollection(colToFetch,null);
            return colToFetch;
        }

It appears we need the EntityFactory to create a collection. We cannot find a way to get to that factory given the EntityBase2 object.
We read a couple posts where CallCreateEntityFactory was used but the method is internal.

Any advice is greatly appreciated. Regards.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Nov-2006 07:23:34   

Use the generic collection EntityCollection<T> rather than the non-generic one. Refer to the following thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=5829

KristianP
User
Posts: 32
Joined: 23-Feb-2005
# Posted on: 16-Nov-2006 13:51:34   
Code: 

Type t = typeof(T);
if(!t.IsAbstract)
{
    T dummy = Activator.CreateInstance(t) as T;
    // and then I have to call a new internal method to call an entity's CreateEntityFactory method: 
    _entityFactoryToUse = dummy.CallCreateEntityFactory();
} 

Frans posted this. Am I missing something here, that method is marked internal, so I do not have access.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Nov-2006 14:38:48   

You don't need to pass a factory; The type passed to the EntityCollection is more than enough. So you don't need the factory code anymore.

Hinkel
User
Posts: 4
Joined: 07-Feb-2005
# Posted on: 16-Nov-2006 14:50:00   

The problem there is the EntityCollection is in the generated code. Our base business service does not know about the generated code. This is why we are looking for the factory to create the colleciton for us.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Nov-2006 15:14:17   

The following is copied from the docs:

.NET 2.0 specific: generics LLBLGen Pro v2.0 supports both .NET 1.x and .NET 2.0. For .NET 2.0, there is both the non-generic EntityCollection class, as known from the generated code for .NET 1.x, and a generic EntityCollection class: EntityCollection(Of TEntity), where TEntity is a class which both implements IEntity2 and derives (indirectly) from EntityBase2, the base class of all adapter Entity classes. The non-generic variant is used for backwards compatibility. The entities themselves use the generic variant, so CustomerEntity.Orders will be of type EntityCollection(Of OrderEntity). In this documentation, the VB.NET way of specifying generics is used, to simplify typing the documentation and to avoid needing to formulate everything twice, plus it's more describing of what it means. So for the people who are unfamiliar with the VB.NET way of defining generics: EntityCollection<B> == EntityCollection(Of B).

KristianP
User
Posts: 32
Joined: 23-Feb-2005
# Posted on: 16-Nov-2006 15:21:13   

I think the question here is if I have an entity type instance, is there anyway to create an entity collection generically. The EntityCollection<T> is located in the db specific project, which the code does not have access to. I believe the next higher up level is EntityCollectionBase2<T>, which is abstract, thus not instantiable. So, unless there is something I am missing here, reflection may be the only way.

Does that make sense?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Nov-2006 15:30:15   

The EntityCollection<TEntity> class is defined in the EntityCollection.cs in the DatabaseGeneric project (you can find it inside the HelperClasses folder)

public partial class EntityCollection<TEntity> : EntityCollectionBase2<TEntity>
        where TEntity : EntityBase2, IEntity2