我正在为我的实体使用 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;
}
}
}
}
这个问题有明确的解决方案吗?