/// <summary> /// ロックします。 /// </summary> public void Enter(ILazyModel model) { lock (SyncRoot) { this.count += 1; } }
/// <summary> /// もし可能であれば、変更されたプロパティの通知を送ります。 /// </summary> internal void RaisePropertyChangedIfNeed(ILazyModel model) { string[] propertyArray = null; lock (SyncRoot) { // ロックされているなら、プロパティの変更通知は送りません。 if (this.count > 0) { return; } if (this.updatedPropertyList.Any()) { // updatedPropertyListはこの後クリアされるため、 // 配列オブジェクトを作っています。 propertyArray = this.updatedPropertyList .Distinct().ToArray(); } this.updatedPropertyList.Clear(); } // 実際に通知を出します。 NotifyPropertyChanged(model, propertyArray); }
/// <summary> /// ILazyModelのプロパティ値変更通知を出します。 /// </summary> private static void LazyModel_RaisePropertyChanged( this ILazyModel self, PropertyChangedEventArgs e) { // 変更プロパティを追加し、もし可能ならそれを実際に通知します。 self.LazyModelObject.AddChangedProperty(e.PropertyName); self.LazyModelObject.RaisePropertyChangedIfNeed(self); }
/// <summary> /// コンストラクタ /// </summary> public LazyModelLock(ILazyModel self, object lockObject) : this(self) { if (lockObject != null) { // ここからロックを開始します。 this.debugLock = new DebugLock(lockObject, false); } }
/// <summary> /// 与えられた全プロパティの更新通知を出します。 /// </summary> private void NotifyPropertyChanged(ILazyModel model, IEnumerable <string> propertyList) { foreach (var propertyName in propertyList) { model.NotifyPropertyChanged( new PropertyChangedEventArgs(propertyName)); } }
/// <summary> /// オブジェクトを破棄します。 /// </summary> public void Dispose() { if (this.debugLock != null) { this.debugLock.Dispose(); this.debugLock = null; } if (this.self != null) { this.self.LazyModelObject.Exit(self); this.self = null; } }
/// <summary> /// 与えられた全プロパティの更新通知を出します。 /// </summary> private void NotifyPropertyChanged(ILazyModel model, IEnumerable <string> propertyList) { // 変更されたプロパティがあれば、変更通知を送ります。 if (propertyList == null) { return; } foreach (var propertyName in propertyList) { model.NotifyPropertyChanged( new PropertyChangedEventArgs(propertyName)); } }
/// <summary> /// 与えられた全プロパティの更新通知を出します。 /// </summary> private void NotifyPropertyChanged(ILazyModel model, IEnumerable<string> propertyList) { // 変更されたプロパティがあれば、変更通知を送ります。 if (propertyList == null) { return; } foreach (var propertyName in propertyList) { model.NotifyPropertyChanged( new PropertyChangedEventArgs(propertyName)); } }
/// <summary> /// ロック解除します。 /// </summary> public void Exit(ILazyModel model) { if (model == null) { throw new ArgumentNullException("model"); } lock (SyncRoot) { if (this.count == 0) { throw new InvalidOperationException( "ロックカウントが0なのでロック解除できません。"); } this.count -= 1; } RaisePropertyChangedIfNeed(model); }
/// <summary> /// もし可能であれば、変更されたプロパティの通知を送ります。 /// </summary> internal void RaisePropertyChangedIfNeed(ILazyModel model) { string[] propertyArray = null; lock (SyncRoot) { // ロックされているなら、プロパティの変更通知は送りません。 if (this.count > 0) { return; } if (this.updatedPropertyList.Any()) { // updatedPropertyListはこの後クリアされるため、 // 配列オブジェクトを作っています。 propertyArray = this.updatedPropertyList .Distinct().ToArray(); this.updatedPropertyList.Clear(); } } // 変更されたプロパティがあれば、変更通知を送ります。 if (propertyArray != null) { NotifyPropertyChanged(model, propertyArray); } // イベントを発火します。 var handler = Interlocked.Exchange(ref FiresOnExit, null); if (handler != null) { Util.SafeCall(() => handler(model, EventArgs.Empty)); } }
/// <summary> /// コンストラクタ /// </summary> public LazyModelLock(ILazyModel self) { self.LazyModelObject.Enter(self); this.self = self; }