使用LINQ-to-SQL实现INotifyPropertyChanged



我正在为我的实体构建一个使用LINQ-to-SQL的Windows Phone 8应用程序。我目前的实现使用简单的INotifyPropertyChanging/INotififyPropertyChanged方法:

[Table]
public class Item : INotifyPropertyChanged, INotifyPropertyChanging
{
    private int _itemId;
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
    public int ItemId
    {
        get { return _itemId; }
        set
        {
            if (_itemId != value)
            {
                NotifyPropertyChanging("ItemId");
                _itemId = value;
                NotifyPropertyChanged("ItemId");
            }
        }
    }
    [Column]
    internal int? _groupId;
    private EntityRef<Board> _group;
    [Association(Storage = "_group", ThisKey = "_groupId", OtherKey = "GroupId", IsForeignKey = true)]
    public Group Group
    {
        get { return _group.Entity; }
        set
        {
            NotifyPropertyChanging("Group");
            _group.Entity = value;
            if (value != null)
            {
                _groupId = value.BoardId;
            }
            NotifyPropertyChanging("Group");
        }
    }
}

我想将属性设置器更改为我的新方法,我在这里创建:http://danrigby.com/2012/04/01/inotifypropertychanged-the-net-4-5-way-revisited/

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
    if (object.Equals(storage, value)) return false;
    this.OnPropertyChanging(propertyName);
    storage = value;
    this.OnPropertyChanged(propertyName);
    return true;
}

对于像ItemId这样的属性很容易实现,但是我不知道如何实现GroupId,因为应该设置在_group上。

这是我的解决方案(尚未测试),但我认为PropertyChanging/PropertyChanged事件将被提前提出:

    public Group Group
    {
        get { return _group.Entity; }
        set
        {
            var group = _group.Entity;
            if (SetProperty(ref group, value))
            {
                _group.Entity = group;
                if (value != null)
                {
                    _groupId = value.GroupId;
                }
            }
        }
    }

这个问题有明确的解决方案吗?

我找到了一个解决方案。只需用另一个实现重载该方法:

    protected T SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value))
        {
            return value;
        }
        NotifyPropertyChanging(propertyName);
        field = value;
        NotifyPropertyChanged(propertyName);
        return value;
    }
    protected T SetProperty<T>(ref EntityRef<T> field, T value, [CallerMemberName] string propertyName = null) where T : class
    {
        NotifyPropertyChanging(propertyName);
        field.Entity = value;
        NotifyPropertyChanged(propertyName);
        return value;
    }

这样写:

    public Group Group
    {
        get { return _board.Entity; }
        set
        {
            if (SetProperty(ref _group, value) != null)
            {
                _groupId = value.GroupId;
            }
        }
    }

相关内容

  • 没有找到相关文章

最新更新