Porting EntityCollection code from LLBLGen 1.x to 2.0

Posts   
 
    
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 18-Apr-2009 00:17:25   

LLBLGen Pro version + buildnr = Version 2.0.0.0 Final (November 6th, 2006) Template = Adapter .NET version = 2.0

I'm trying to port some code that another programmer wrote using LLBLGen 1.x to LLBLGen 2.0 It appears to be a routine for sorting collections of entities.


        public static void Sort(IEntityCollection2 collection, IComparer comparer, bool reverse)
        {
            EntityCollection
            ArrayList tempArray = new ArrayList();

            tempArray.AddRange((IList) collection);
            tempArray.Sort(comparer);

            ((EntityCollectionBase2)collection).Clear();
            
            if (!reverse)
            {
                for (int i = 0; i < tempArray.Count; i++)
                    collection.Add((IEntity2) tempArray[i]);
            }
            else
            {
                for (int i = tempArray.Count - 1; i >= 0; i--)
                    collection.Add((IEntity2) tempArray[i]);
            }
        }

This code will not compile when using LLBLGen Version 2.0. I get this error:

Using the generic type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2<TEntity>' requires '1' type arguments

for this line:


            ((EntityCollectionBase2)collection).Clear();

It appears that EntityCollectionBase2 is now a generic collection while in version 1.x it wasn't.

So I was wondering how I should rewrite the method so it compiles in version 2.x. I notice there is a class called EntityCollectionNonGeneric, but I'd prefer to use generics.

So I changed the line to what I think would retain the behavior of the method. As a relative newbie, I guess I'm just looking for confirmation.


            ((EntityCollectionBase2<EntityBase2>)collection).Clear();

I have some questions about this code too.

Looking at the documentation it says that IEntityCollection2 is the Interface for the EntityCollection2 type. But I don't see the EntityCollection2 type anywhere in the documentation. Is that a mistake in the documentation? Is it really supposed to say EntityCollectionBase2?

Why does he cast collection to EntityCollectionBase2? Couldn't he just call Clear() without it since it is one of the methods specified in the IEntityCollection2 interface?

Why does he pass in IEntityCollection2 instead of EntityCollectionBase2? Is that just good programming practice? I don't think we would ever be making another collection class that implements IEntityCollection2.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Apr-2009 05:49:30   

clint wrote:

It appears that EntityCollectionBase2 is now a generic collection while in version 1.x it wasn't.

So I was wondering how I should rewrite the method so it compiles in version 2.x. I notice there is a class called EntityCollectionNonGeneric, but I'd prefer to use generics.

So I changed the line to what I think would retain the behavior of the method. As a relative newbie, I guess I'm just looking for confirmation.


            ((EntityCollectionBase2<EntityBase2>)collection).Clear();

That code modification is ok. In v1.x _IEntityCollection2 _didn't have the Clear() method. That's the why of his cast. Indeed you now (in v2.0) should do that this way:

collection.Clear();

clint wrote:

Looking at the documentation it says that IEntityCollection2 is the Interface for the EntityCollection2 type. But I don't see the EntityCollection2 type anywhere in the documentation. Is that a mistake in the documentation? Is it really supposed to say EntityCollectionBase2?

It is not a mistake, it is just _IEntityCollection2 _isn't used in the basic documentation and exmaples. You should find more details if you download the ReferenceManual.

clint wrote:

Why does he cast collection to EntityCollectionBase2? Couldn't he just call Clear() without it since it is one of the methods specified in the IEntityCollection2 interface?

In v1.x the _IEntityCollection2 _interface didn't have a a Clear() method, _EntityCollectionBase2 _did.

clint wrote:

Why does he pass in IEntityCollection2 instead of EntityCollectionBase2? Is that just good programming practice? I don't think we would ever be making another collection class that implements IEntityCollection2.

Passing _EntityCollectionBase2 _would work as well. The _IEntityCollection2 _defines typed basic collection behavior. So, for the nature of the method is more standard and practical to pass the object as _IEntityCollection2 _(_EntityCollectionBase2 _implements IEntityCollection2).

David Elizondo | LLBLGen Support Team
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 20-Apr-2009 17:03:31   

Thanks for your help daelmo.

daelmo wrote:

clint wrote:

Looking at the documentation it says that IEntityCollection2 is the Interface for the EntityCollection2 type. But I don't see the EntityCollection2 type anywhere in the documentation. Is that a mistake in the documentation? Is it really supposed to say EntityCollectionBase2?

It is not a mistake, it is just _IEntityCollection2 _isn't used in the basic documentation and exmaples. You should find more details if you download the ReferenceManual.

The reference manual has information about IEntityCollection2 but it doesn't seem to have any information about EntityCollection2.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Apr-2009 05:46:14   

Has information about IEntityCollection2 (the interface) and EntityCollectioBase2 (the base classs) simple_smile

David Elizondo | LLBLGen Support Team