Search code examples
c#wpfsilverlightwcf-ria-servicesdomainservices

How to notify EntityChangeSet in DomainServices Silverlight


First at all, my English is weak, hope you can understad! I'm developing a silverlight application using DomainServices(DomainContext Class - Namespace: System.ServiceModel.DomainServices.Client) to retrieve and update data. I have some Properties:

DomainContextVBHS _Service = new DomainContextVBHS();


public int AddedEntitiesCount
{
    get{ return _Service.EntityContainer.GetChanges().AddedEntities.Count; }
}

public int ModifiedEntitiesCount
{
    get{ return _Service.EntityContainer.GetChanges().ModifiedEntities.Count; }
}
public int RemovedEntitiesCount
{
    get{ return _Service.EntityContainer.GetChanges().RemovedEntities.Count; }
}

How can i notify them on UI when _Service get any changes?


Solution

  • Update: Sample code here: https://dl.dropboxusercontent.com/u/8424800/StackOverflowHasChanges.zip

    I had a quick look at the source using the decompiler. There's no official way available. Fortunately, if you are willing to listen to all property changed events on all the inner entity sets, you should be able to achieve what you are after.

    public class Sample : INotifyPropertyChanged {

        SampleDomainContext _Service = new SampleDomainContext();
    
        public Sample()
        {
            _Service.Load(_Service.GetCustomersQuery());
    
            _Service.Customers.PropertyChanged += PropChanged;
            //_Service.Orders.PropertyChanged += PropChanged;
            // etc.
        }
    
        public int AddedEntitiesCount
        {
            get { return _Service.EntityContainer.GetChanges().AddedEntities.Count; }
        }
    
        public int ModifiedEntitiesCount
        {
            get { return _Service.EntityContainer.GetChanges().ModifiedEntities.Count; }
        }
        public int RemovedEntitiesCount
        {
            get { return _Service.EntityContainer.GetChanges().RemovedEntities.Count; }
        }
    
        public void PropChanged(object sender, PropertyChangedEventArgs e)
        {
            var pc = this.PropertyChanged;
            if (pc != null)
            {
                pc(this, new PropertyChangedEventArgs("AddedEntitiesCount"));
                pc(this, new PropertyChangedEventArgs("ModifiedEntitiesCount"));
                pc(this, new PropertyChangedEventArgs("RemovedEntitiesCount"));
                // etc
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
    
    }