Adding to collection

Posts   
 
    
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 21-Jun-2007 21:07:18   

What is considered the best practice for doing something every time an entity is added to another entity's collection? So If I have LineItem (1:n) LineItemEntry, and I do LineItem.LineItemEntries.Add(new LineItemEntry), where would be a good place to put "extra" code for this Add? For example, if I wanted to update some count in the LineItem entity automatically...

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 22-Jun-2007 10:09:32   

You can have a Count property inside the LineItem, that on Get calculates its value from the attached LineItemEntires, so whenever you add a LineItemEntry and access the Count property, it will show the updated value.

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 22-Jun-2007 17:05:54   

Walaa wrote:

You can have a Count property inside the LineItem, that on Get calculates its value from the attached LineItemEntires, so whenever you add a LineItemEntry and access the Count property, it will show the updated value.

I'm thinking more of a place to actually set a value in the LineItem when a LineItemEntry is added or removed. So that if the LineItem is fetched separately from the collection, this value will be there. This is more of a general question (LineItem, LineItemEntry, and the count are just examples)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jun-2007 07:44:59   

I would make it at the <yourEntity>.cs class of your DBGenericProject (assuming Adapter scenario) and using an eventHandler for the mentioned collection property.

...
...

/// <summary> Initializes the class members</summary>
protected virtual void InitClassMembers()
{

    _customerCustomerDemo = null;
    _orders = null;
    _customerDemographicsCollectionViaCustomerCustomerDemo = null;
    _employeesCollectionViaOrders = null;
    _shippersCollectionViaOrders = null;

    
    // __LLBLGENPRO_USER_CODE_REGION_START InitClassMembers
    this.Orders.EntityAdded += new EventHandler<CollectionChangedEventArgs>(Orders_EntityAdded);
    // __LLBLGENPRO_USER_CODE_REGION_END
}

...
...

#region Custom Entity code

// __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCode
void Orders_EntityAdded(object sender, CollectionChangedEventArgs e)
{
    // your code...
}
// __LLBLGENPRO_USER_CODE_REGION_END
#endregion
David Elizondo | LLBLGen Support Team
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 26-Jun-2007 17:44:00   

Ok, thanks! I still use version 1.0.2005.1 so I will have to manually add this event, but I get the point simple_smile

JSobell
User
Posts: 145
Joined: 07-Jan-2006
# Posted on: 27-Jun-2007 02:33:14   

Better (and simpler) to override the underlying call, rather than trap the event:

#region Custom EntityCollection code
        
// __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCollectionCode

    protected override void OnEntityAdded(AnalysisCodesEntity entityToAdd)
    {
        Console.WriteLine("Adding entity, so do whatever in here");
        base.OnEntityAdded(entityToAdd);
    }

// __LLBLGENPRO_USER_CODE_REGION_END
        
#endregion

Cheers, Jason

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Jun-2007 07:43:46   

That's ok wink . But at _OnEntityAdded _you have access to the collection itself, no to the parent entity. If you want do something if a entity is added to Customer.Orders Collection is better trap the event.

David Elizondo | LLBLGen Support Team