3

Я создаю приложение Windows Phone 8 с использованием LINQ-to-SQL для своих объектов. Моя текущая реализация использует простые INotifyPropertyChanging/INotififyPropertyChanged методы:Реализация INotifyPropertyChanged с использованием LINQ-to-SQL

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

Есть ли прозрачный раствор для этого вопроса?

ответ

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