Child item knowing its parent collection

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 13-Aug-2013 11:18:33   

If I have an InvoiceEntity class which holds a collection of InvoiceItemEntity and InvoiceItemEntity implements ICustomDisplayOrder which looks like this:-

    public interface ICustomDisplayOrder
    {
        int? DisplayOrder { get; set; }
    }

(DisplayOrder is an entity field BTW)

What I basically want to do is have DisplayOrder automatically get set when an xxxItem gets added to its parent collection. It should be set to the parent collection's .Count property to put it at the end of the list. So it needs to obtain the parent collection it is contained in somehow.

I have a few situations like the above and want to find a solution that can be generically reused.

I am happy to extend ICustomDisplayOrder or add an interface to the parent entity or write custom code in either parent or child to achieve this.

Updating when saving is not the best solution since the items will be displayed in a grid in the meantime. The solution needs to be able to cope with entities being added to the collection directly or via AddNew or AddRange and binding etc.

There is a _parentCollection on EntityBase2 but it has been locked down so I don't know if that is usable (happy to use reflection if it is).

What can you suggest? LLBLGen 4.0 July 18th

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Aug-2013 21:45:26   

Please use OnRelatedEntitySet() on the entity in hand.

protected internal virtual void OnRelatedEntitySet(
    IEntityCore relatedEntity,
    string fieldName
)

You can cast the relatedEntity to the parent entity, and access the required collection to get the count.

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 14-Aug-2013 11:08:57   

Thanks Walaa, that helps

I don't understand why ParentCollection is internal, though. Because it is, I have to use 'inside' knowledge' of the parent class and the parent's collection property and then repeat in many places.

If ParentCollection was accessible then I could just write the code once in my base entity class. Something along the lines of this pseudo code:

IF relatedEntity IS NEW AND relatedEntity is ICustomDisplayOrder AND ((ICustomDisplayOrder) relatedEntity).DisplayOrder IS NULL AND ParentCollection != null THEN ((ICustomDisplayOrder) relatedEntity).DisplayOrder = ParentCollection.Count.

I really am tempted to make this property visible unless there is a good reason why it is internal - are there existing plans to change how this works?

Cheers Simon

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Aug-2013 01:02:42   

ParentCollection is used in Databinding, I don't think you should be using it.

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 15-Aug-2013 08:55:51   

I think you might be right. I Found another way for my own code.

I already had changed EntityCollection<T>.OnListChanged to call EntityBase2 OnMemberCollectionChanged on its owning entity to 'pass on' its changes.

The parameters passed include both the collection itself and the entity that was added/modified - all I need to do the job.

For anyone else doing this with grid binding, the trick is to force fire a ListChanged.Reset after updating the DisplayOrders. Due to interactions with IEditObject, IBindingList and others and timings of events, I found that events sent by EntityCollection and EntityView were being suppressed so the grid didn't know about the changes until you left the editing row.

        public void RaiseResetEvent(bool force = false)
        {
            if (listChangedFireLock && !force) return;
            
            base.OnListChanged(-1, ListChangedType.Reset);
        }

Now works fine - I don't have to write any more code, just add ICustomDisplayOrder to any entity class.