如何检测WCF模型上的更改

本文关键字:模型 WCF 何检测 检测 | 更新日期: 2023-09-27 18:26:22

我有这个ViewModel:

public class CombustiblesViewModel
{        
    public List<CombustiblesWCFModel> Combustibles{ get; set; }        
    public CombustiblesViewModel()
    {
        Combustibles = _svc.Combustibles_List(sTicket);                            
    }      
}  

Combustibles_List从WCF服务返回列表。我需要的是跟踪每个CombustiblesWCFModel对象上的更改。所以我用这个代码扩展我的模型:

public CombustiblesWCFModel()
    {
        this.PropertyChanged += ChangedRow;   
    }
    private void ChangedRow(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "HasChanges") return;
        CombustiblesWCFModel p = (CombustiblesWCFModel)sender;            
        // Should Rise HasChanges Property on model, but it doesn't work
        p.HasChanges = true;
    }
    private bool _haschanges;
    public bool HasChanges
    {
        get
        {
            return _haschanges;
        }
        set
        {
            _haschanges = value;
            this.RaisePropertyChanged("HasChanges");
        }
    }
}

我的问题是HasChanges总是错误的。我认为,当从WCF服务返回模型时,PropertyChanged事件被高估了。

问题

那么,如何检测视图模型集合中每个对象的模型更改?

如何检测WCF模型上的更改

WCF合约是序列化对象,它们应该被视为不可变的。

您想要做的是拥有一个本地缓存,将更改合并到有状态副本中。这可能很简单,只需将数据从约定映射到正确实现INotifyPropertyChanged的类即可。对于像AutoMapper这样的简单映射工具来说,这是一条很长的路。

对于更复杂的合并和困难,有一个Compare.NETObjects项目将提供细粒度的信息。

其他可能使用的工具是Reactive Extensions可观察集合,您可以在其中插入数据契约并订阅更改。

另一种选择是实际转移到具有WCF的发布者-订阅者模型。IDesign和Juval Lowy提供了Pub-Sub与WCF的现成实现。

如果您的集合正在更改,并且您需要获得更新的集合,则可以使用ObservableCollection来代替自动实现NotifyPropertyChanged事件的List。