首页 文章

使用LINQ-to-SQL实现INotifyPropertyChanged

提问于
浏览
3

我正在为我的实体使用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");
        }
    }
}

我想将属性setter更改为我的新方法,我在这里创建: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.Entity上设置,这不能作为引用传递 .

这是我的解决方法(尚未测试),但我认为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;
                }
            }
        }
    }

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

1 回答

  • 2

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

    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;
                }
            }
        }
    

相关问题