POCOs and multiple ViewModels pointing to the same POCO?

こ雲淡風輕ζ 提交于 2019-12-09 04:38:41

Assuming that your POCO can't implement INotifyPropertyChanged, you could use a mediator pattern to alert other view models when a POCO is changed:

public interface ICareWhenAModelChanges<T>
{
    void ModelUpdated(T updatedModel);
}

public class ModelChangeMediator<T>
{
    private List<ICareWhenAModelChanges<T>> _listeners = new List<ICareWhenAModelChanges<T>>();

    public void Register(ICareWhenAModelChanges<T> listener)
    {
        _listeners.Add(listener);
    }

    public void NotifyThatModelIsUpdated(T updatedModel)
    {
        foreach (var listener in _listeners) listener.ModelUpdated(updatedModel);
    }
}

Your view model can then implement the ICareWhenAModelChanges<T> interface, register itself with a shared instance of the mediator (acquired through either a singleton or, better, some kind of DI/IoC framework) and do whatever it needs to in the ModelUpdated method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!