best place to extend collections

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 28-Sep-2006 06:52:52   

I have a target per entity hierarchy. Lets say A->B->C.

That causes collections to be created - AEntityCollection, BEntityCollection and CEntityCollection.

At various times, I have a selection of records loaded from any of the collections. On these collections, there is at most one record that has a fieldX==true. So, if I were to create a property of the collection called ActiveRecord, where that property used an EntityView to locate that record and return it - where would I put that code?

Note, fieldX is a field that is in AEntity (so the others inherit it). Ideally, I would like to have this code in one place to share among all the collections - however, the collections do not inherit from each other - so........

Code would be like:

public CEntity ActiveRecord
{
    get
    {
        CEntity activeRec = null;
        if (this.Count > 0)
        {
            EntityView<CEntity> activeView = new EntityView<CEntity>(this, CFields.FieldX == true);
            if (activeView.Count > 0)
            {
                activeRec = activeView[0];
            }
        }

        return activeRec;
    }
}

For now I have just duplicated the code in each collection class....

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Sep-2006 07:35:33   

I don't see a way to have it like a property in one place since collections don't inherit from each other.

Instead of a property you should create a BL method that takes an entityCollection and return the Active Entity.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 28-Sep-2006 09:19:25   

What kind of entity collection?

The collection types are EntityCollection<BEntity>, EntityCollection<CEntity>, etc. A BL method that takes EntityCollection<AEntity> does not accept the other collections as parameters....

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 28-Sep-2006 10:45:32   

No, but you can make the parameter be of type IEntityCollection.

I couldn't make the collections inherit from eachother as C# and VB.NET don't support co-variance (List<string> doesn't derive from List<object>) and the classes are named and non-generic as backwardscompatibility was a mandatory goal.

You also have the option to include it into the collection class with an include template, which only emits the code if the currententityname equals to a small list of names (you can use an .lpt template for this, you can use lpt templates for including code into tdl templates).

Frans Bouma | Lead developer LLBLGen Pro