Replacing an Entity in a Bound EntityCollection

Posts   
 
    
Lusid
User
Posts: 9
Joined: 02-Jul-2007
# Posted on: 21-Oct-2007 23:23:32   

Hello,

I know there have been LOTS of issues concerning databinding and the different events that get raised in the past, and I've tried my best to weed through them and find an answer to my problem, but with no luck.

Basically, I have a duplex WCF service that I'm using to provide real-time change notifications to each client, and I'm only using EntityCollection on the client side for databinding purposes while the actual data access is being performed using the DataAccessAdapter on the server side.

This is actually working WONDERFULLY, and I've been VERY pleased with how well LLBLGen is holding up its end of the bargain. One issue remains however, and while I've found a workaround, I can't help but wonder if I'm doing something wrong.

I'm using DevExpress, and I have a tree component bound to an entity collection (I've also tried doing it through a binding source with the same results). When a client updates a record on the service, it sends out a NotifyEntityChanged WCF callback event and passes the changed entity to each client. When the client receives it, it attempts to find the entity in the local collection using an IndexOf, and then REPLACE the entity with the one provided by the service. Here's sample code:


        private EntityCollection<PropertyRootEntity> _propertyRoots;
        public EntityCollection<PropertyRootEntity> PropertyRoots
        {
            get { return _propertyRoots; }
        }

        public PropertyServiceCallback()
        {
            _propertyRoots = new EntityCollection<PropertyRootEntity>(new PropertyRootEntityFactory());
            _propertyRoots.DefaultView.RaisesItemChangedEvents = true;
        }

        public void NotifyPropertyChanged(SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 entity)
        {
            PropertyRootEntity propertyRoot = entity as PropertyRootEntity;
            if (_propertyRoots.Contains(propertyRoot))
            {
                int idx = _propertyRoots.IndexOf(propertyRoot);
                if (!_propertyRoots[idx].IsDirty)
                {
                    _propertyRoots[idx] = propertyRoot;
                }
                else
                { // TODO: Handle "partial updating" of a record that is being edited by client.
                }
            }
            else
            {
                _propertyRoots.Add(propertyRoot);
            }
        }

The problem is that when an entity within the collection is replaced, no events fire to notify the tree that it needs to update. I have gotten around this by adding the following method to EntityCollection<TEntity> and calling it directly after the replacing line.


        #region Custom EntityCollection code
        
        // __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCollectionCodeGeneric
        public void Refresh(int idx)
        {
            this.OnListChanged(idx, ListChangedType.ItemChanged);
        }
        // __LLBLGENPRO_USER_CODE_REGION_END
        
        #endregion

This allows me to force an OnListChanged whenever I like... but is there a better way to do this? It seems... clunky. And for the record, I know passing entities back and forth through WCF is bad practice... simple_smile

Thanks, Marc

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Oct-2007 07:36:08   

Hi Marc,

Are you sure the debugger enters here?

if (_propertyRoots.Contains(propertyRoot))
{
...

and... Where did you add the ListChanged event handler?

David Elizondo | LLBLGen Support Team