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

我想将属性设置器更改为我的新方法,我在这里创建: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;
                }
            }
        }
    }

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

4

1 回答 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;
            }
        }
    }
于 2013-04-05T15:59:28.697 回答