1- // Copyright (C) 2003-2010 Xtensive LLC.
1+ // Copyright (C) 2003-2021 Xtensive LLC.
22// All rights reserved.
33// For conditions of distribution and use, see license.
44// Created by: Dmitri Maximov
77using System ;
88using System . Collections . Generic ;
99using System . Diagnostics ;
10+ using System . Runtime . Serialization ;
1011using Xtensive . Core ;
1112
12- using Xtensive . Collections ;
13-
1413namespace Xtensive . Collections
1514{
1615 /// <summary>
@@ -19,32 +18,38 @@ namespace Xtensive.Collections
1918 [ Serializable ]
2019 [ DebuggerDisplay ( "Count = {Count}" ) ]
2120 public class CollectionBase < TItem > : CollectionBaseSlim < TItem > ,
22- ICollectionChangeNotifier < TItem >
21+ ICollectionChangeNotifier < TItem > ,
22+ IDeserializationCallback
2323 {
24- [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
24+ [ NonSerialized , DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
2525 private EventHandler < ChangeNotifierEventArgs > itemChangedHandler ;
26- [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
26+
27+ [ NonSerialized , DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
2728 private EventHandler < ChangeNotifierEventArgs > itemChangingHandler ;
28- [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
29+
30+ [ NonSerialized , DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
2931 private CollectionEventBroker < TItem > eventBroker ;
3032
3133 /// <summary>
3234 /// Gets or sets the element at the specified index.
3335 /// </summary>
3436 /// <param name="index">The zero-based index of the element to get or set.</param>
3537 /// <returns>The element at the specified index.</returns>
36- public override TItem this [ int index ] {
37- get {
38- return base [ index ] ;
39- }
38+ public override TItem this [ int index ]
39+ {
40+ get => base [ index ] ;
4041 set {
41- if ( index < 0 || index >= Count )
42- throw new IndexOutOfRangeException ( ) ;
42+ if ( index < 0 || index >= Count ) {
43+ throw new IndexOutOfRangeException ( nameof ( index ) ) ;
44+ }
45+
4346 OnValidate ( value ) ;
44- TItem oldValue = base [ index ] ;
45- if ( ( oldValue == null && value == null ) ||
46- ( oldValue != null && oldValue . Equals ( value ) ) )
47+ var oldValue = base [ index ] ;
48+ if ( ( oldValue == null && value == null ) ||
49+ ( oldValue != null && oldValue . Equals ( value ) ) ) {
4750 return ;
51+ }
52+
4853 OnChanging ( ) ;
4954 OnRemoving ( oldValue , index ) ;
5055 OnInserting ( value , index ) ;
@@ -68,8 +73,10 @@ public override TItem this[int index] {
6873 /// <param name="value">Item to insert.</param>
6974 public override void Insert ( int index , TItem value )
7075 {
71- if ( index < 0 || index > Count )
76+ if ( index < 0 || index > Count ) {
7277 throw new ArgumentOutOfRangeException ( "index" ) ;
78+ }
79+
7380 OnValidate ( value ) ;
7481 OnChanging ( ) ;
7582 OnInserting ( value , index ) ;
@@ -91,14 +98,16 @@ public override void Insert(int index, TItem value)
9198 /// <param name="index">The zero-based index of the element to remove.</param>
9299 public override void RemoveAt ( int index )
93100 {
94- if ( index < 0 || index >= Count )
101+ if ( index < 0 || index >= Count ) {
95102 throw new ArgumentOutOfRangeException ( "index" ) ;
96- TItem o = base [ index ] ;
97- OnValidate ( o ) ;
103+ }
104+
105+ var item = base [ index ] ;
106+ OnValidate ( item ) ;
98107 OnChanging ( ) ;
99- OnRemoving ( o , index ) ;
108+ OnRemoving ( item , index ) ;
100109 base . RemoveAt ( index ) ;
101- OnRemoved ( o , index ) ;
110+ OnRemoved ( item , index ) ;
102111 OnChanged ( ) ;
103112 }
104113
@@ -110,8 +119,11 @@ public override void Clear()
110119 {
111120 OnChanging ( ) ;
112121 OnClearing ( ) ;
113- foreach ( TItem item in Items )
122+
123+ foreach ( var item in Items ) {
114124 TryUnsubscribe ( item ) ;
125+ }
126+
115127 base . Clear ( ) ;
116128 OnCleared ( ) ;
117129 OnChanged ( ) ;
@@ -124,7 +136,7 @@ public override void Clear()
124136 public override void Add ( TItem value )
125137 {
126138 OnValidate ( value ) ;
127- int index = Count ;
139+ var index = Count ;
128140 OnChanging ( ) ;
129141 OnInserting ( value , index ) ;
130142 base . Add ( value ) ;
@@ -141,8 +153,9 @@ public override void Add(TItem value)
141153 /// <inheritdoc/>
142154 public override void AddRange ( IEnumerable < TItem > collection )
143155 {
144- foreach ( TItem item in collection )
156+ foreach ( var item in collection ) {
145157 Add ( item ) ;
158+ }
146159 }
147160
148161 /// <summary>
@@ -152,9 +165,11 @@ public override void AddRange(IEnumerable<TItem> collection)
152165 public override bool Remove ( TItem value )
153166 {
154167 OnValidate ( value ) ;
155- int index = IndexOf ( value ) ;
156- if ( index < 0 )
168+ var index = IndexOf ( value ) ;
169+ if ( index < 0 ) {
157170 return false ;
171+ }
172+
158173 OnChanging ( ) ;
159174 OnRemoving ( value , index ) ;
160175 base . RemoveAt ( index ) ;
@@ -165,17 +180,14 @@ public override bool Remove(TItem value)
165180
166181 private CollectionEventBroker < TItem > EventBroker {
167182 get {
168- if ( eventBroker == null )
183+ if ( eventBroker == null ) {
169184 eventBroker = new CollectionEventBroker < TItem > ( ) ;
185+ }
170186 return eventBroker ;
171187 }
172188 }
173189
174- private bool EventBrokerExists {
175- get {
176- return eventBroker != null ;
177- }
178- }
190+ private bool EventBrokerExists => eventBroker != null ;
179191
180192 /// <summary>
181193 /// Tries to subscribe the collection on
@@ -184,8 +196,7 @@ private bool EventBrokerExists {
184196 /// <param name="item">The item to try.</param>
185197 protected void TrySubscribe ( TItem item )
186198 {
187- IChangeNotifier notifier = item as IChangeNotifier ;
188- if ( notifier != null ) {
199+ if ( item is IChangeNotifier notifier ) {
189200 notifier . Changing += itemChangingHandler ;
190201 notifier . Changed += itemChangedHandler ;
191202 }
@@ -198,8 +209,7 @@ protected void TrySubscribe(TItem item)
198209 /// <param name="item">The item to try.</param>
199210 protected void TryUnsubscribe ( TItem item )
200211 {
201- IChangeNotifier notifier = item as IChangeNotifier ;
202- if ( notifier != null ) {
212+ if ( item is IChangeNotifier notifier ) {
203213 notifier . Changing -= itemChangingHandler ;
204214 notifier . Changed -= itemChangedHandler ;
205215 }
@@ -213,8 +223,9 @@ protected void TryUnsubscribe(TItem item)
213223 /// </summary>
214224 protected virtual void OnChanging ( )
215225 {
216- if ( EventBrokerExists )
226+ if ( EventBrokerExists ) {
217227 EventBroker . RaiseOnChanging ( this , new ChangeNotifierEventArgs ( "Changing" ) ) ;
228+ }
218229 }
219230
220231 /// <summary>
@@ -223,8 +234,9 @@ protected virtual void OnChanging()
223234 /// </summary>
224235 protected virtual void OnChanged ( )
225236 {
226- if ( EventBrokerExists )
237+ if ( EventBrokerExists ) {
227238 EventBroker . RaiseOnChanged ( this , new ChangeNotifierEventArgs ( "Changed" ) ) ;
239+ }
228240 }
229241
230242 /// <summary>
@@ -233,8 +245,9 @@ protected virtual void OnChanged()
233245 /// </summary>
234246 protected virtual void OnClearing ( )
235247 {
236- if ( EventBrokerExists )
248+ if ( EventBrokerExists ) {
237249 EventBroker . RaiseOnClearing ( this , new ChangeNotifierEventArgs ( "Clearing" ) ) ;
250+ }
238251 }
239252
240253 /// <summary>
@@ -243,8 +256,9 @@ protected virtual void OnClearing()
243256 /// </summary>
244257 protected virtual void OnCleared ( )
245258 {
246- if ( EventBrokerExists )
259+ if ( EventBrokerExists ) {
247260 EventBroker . RaiseOnCleared ( this , new ChangeNotifierEventArgs ( "Cleared" ) ) ;
261+ }
248262 }
249263
250264 /// <summary>
@@ -255,8 +269,9 @@ protected virtual void OnCleared()
255269 /// <param name="value">The value to insert.</param>
256270 protected virtual void OnInserting ( TItem value , int index )
257271 {
258- if ( EventBrokerExists )
272+ if ( EventBrokerExists ) {
259273 EventBroker . RaiseOnInserting ( this , new CollectionChangeNotifierEventArgs < TItem > ( value , index ) ) ;
274+ }
260275 }
261276
262277 /// <summary>
@@ -268,8 +283,9 @@ protected virtual void OnInserting(TItem value, int index)
268283 protected virtual void OnInserted ( TItem value , int index )
269284 {
270285 TrySubscribe ( value ) ;
271- if ( EventBrokerExists )
286+ if ( EventBrokerExists ) {
272287 EventBroker . RaiseOnInserted ( this , new CollectionChangeNotifierEventArgs < TItem > ( value , index ) ) ;
288+ }
273289 }
274290
275291 /// <summary>
@@ -280,8 +296,9 @@ protected virtual void OnInserted(TItem value, int index)
280296 /// <param name="value">The element to remove.</param>
281297 protected virtual void OnRemoving ( TItem value , int index )
282298 {
283- if ( EventBrokerExists )
299+ if ( EventBrokerExists ) {
284300 EventBroker . RaiseOnRemoving ( this , new CollectionChangeNotifierEventArgs < TItem > ( value , index ) ) ;
301+ }
285302 }
286303
287304 /// <summary>
@@ -293,8 +310,9 @@ protected virtual void OnRemoving(TItem value, int index)
293310 protected virtual void OnRemoved ( TItem value , int index )
294311 {
295312 TryUnsubscribe ( value ) ;
296- if ( EventBrokerExists )
313+ if ( EventBrokerExists ) {
297314 EventBroker . RaiseOnRemoved ( this , new CollectionChangeNotifierEventArgs < TItem > ( value , index ) ) ;
315+ }
298316 }
299317
300318 /// <summary>
@@ -309,8 +327,10 @@ protected virtual void OnRemoved(TItem value, int index)
309327 /// </remarks>
310328 protected virtual void OnValidate ( TItem value )
311329 {
312- if ( EventBrokerExists )
330+ if ( EventBrokerExists ) {
313331 EventBroker . RaiseOnValidate ( this , new CollectionChangeNotifierEventArgs < TItem > ( value ) ) ;
332+ }
333+
314334 ArgumentValidator . EnsureArgumentNotNull ( value , "value" ) ;
315335 }
316336
@@ -321,8 +341,9 @@ protected virtual void OnValidate(TItem value)
321341 /// <param name="e">The <see cref="ChangeNotifierEventArgs"/> instance containing the event data.</param>
322342 protected virtual void OnItemChanging ( object sender , ChangeNotifierEventArgs e )
323343 {
324- if ( EventBrokerExists )
344+ if ( EventBrokerExists ) {
325345 EventBroker . RaiseOnItemChanging ( this , new CollectionChangeNotifierEventArgs < TItem > ( ( TItem ) sender ) ) ;
346+ }
326347 }
327348
328349 /// <summary>
@@ -332,8 +353,9 @@ protected virtual void OnItemChanging(object sender, ChangeNotifierEventArgs e)
332353 /// <param name="e">The <see cref="ChangeNotifierEventArgs"/> instance containing the event data.</param>
333354 protected virtual void OnItemChanged ( object sender , ChangeNotifierEventArgs e )
334355 {
335- if ( EventBrokerExists )
356+ if ( EventBrokerExists ) {
336357 EventBroker . RaiseOnItemChanged ( this , new CollectionChangeNotifierEventArgs < TItem > ( ( TItem ) sender ) ) ;
358+ }
337359 }
338360
339361 #endregion
@@ -448,13 +470,14 @@ private void Initialize()
448470 {
449471 itemChangingHandler = OnItemChanging ;
450472 itemChangedHandler = OnItemChanged ;
451- foreach ( TItem item in this )
473+ foreach ( var item in this ) {
452474 TrySubscribe ( item ) ;
475+ }
453476 }
454477
455478
456479 // Constructors
457-
480+
458481 /// <summary>
459482 /// Initializes a new instance of this type.
460483 /// </summary>
@@ -482,5 +505,7 @@ public CollectionBase(IEnumerable<TItem> collection)
482505 {
483506 Initialize ( ) ;
484507 }
508+
509+ void IDeserializationCallback . OnDeserialization ( object sender ) => Initialize ( ) ;
485510 }
486511}
0 commit comments