Skip to content

Commit 50dcf15

Browse files
#4622 Implement virtual OnDeserialized methods (#4662)
1 parent bee73b7 commit 50dcf15

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

Source/Csla.test/Serialization/SerializationTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,16 @@ public void TimeOnlySerialization()
651651
var clone = obj.Clone();
652652
Assert.AreEqual(obj.TimeOnly, clone.TimeOnly);
653653
}
654+
655+
[TestMethod]
656+
public void NotifyOnDeserializationTest()
657+
{
658+
var portal = _testDIContext.CreateDataPortal<NotifyOnDeserialization>();
659+
var obj = portal.Create();
660+
obj.Data = "test data";
661+
var clone = obj.Clone();
662+
Assert.AreEqual("OnDeserialized", TestResults.GetResult("OnDeserialized"));
663+
}
654664
}
655665

656666
[Serializable]
@@ -712,4 +722,22 @@ private void Create()
712722
{ }
713723
}
714724

725+
[Serializable]
726+
public class NotifyOnDeserialization : BusinessBase<NotifyOnDeserialization>
727+
{
728+
public static readonly PropertyInfo<string> DataProperty = RegisterProperty<string>(nameof(Data));
729+
public string Data
730+
{
731+
get => GetProperty(DataProperty);
732+
set => SetProperty(DataProperty, value);
733+
}
734+
[Create]
735+
private void Create()
736+
{ }
737+
protected override void OnDeserialized()
738+
{
739+
base.OnDeserialized();
740+
TestResults.AddOrOverwrite("OnDeserialized", "OnDeserialized");
741+
}
742+
}
715743
}

Source/Csla/Core/BusinessBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,15 @@ void ISerializationNotification.Deserialized()
14241424
FieldManager.SetPropertyList(GetType());
14251425
InitializeBusinessRules();
14261426
FieldDataDeserialized();
1427+
OnDeserialized();
1428+
}
1429+
1430+
/// <summary>
1431+
/// Invoked after the object has been deserialized.
1432+
/// </summary>
1433+
protected virtual void OnDeserialized()
1434+
{
1435+
// do nothing by default
14271436
}
14281437

14291438
#endregion

Source/Csla/Core/ExtendedBindingList.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,18 @@ void ISerializationNotification.Deserialized()
279279
// don't rehook events here, because the MobileFormatter has
280280
// created new objects and so the lists will auto-subscribe
281281
// the events;
282+
OnDeserialized();
282283
}
283284

285+
/// <summary>
286+
/// Invoked after the object has been deserialized.
287+
/// </summary>
288+
protected virtual void OnDeserialized()
289+
{
290+
// do nothing by default
291+
}
292+
293+
284294
[NonSerialized]
285295
[NotUndoable]
286296
private EventHandler<ChildChangedEventArgs> _childChangedHandlers;

Source/Csla/Core/ObservableBindingList.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,18 @@ void ISerializationNotification.Deserialized()
382382
// don't rehook events here, because the MobileFormatter has
383383
// created new objects and so the lists will auto-subscribe
384384
// the events
385+
OnDeserialized();
385386
}
386387

388+
/// <summary>
389+
/// Invoked after the object has been deserialized.
390+
/// </summary>
391+
protected virtual void OnDeserialized()
392+
{
393+
// do nothing by default
394+
}
395+
396+
387397
#endregion
388398

389399
#region Child Change Notification

Source/Csla/ReadOnlyBase.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,16 +511,19 @@ protected virtual void Child_OnDataPortalException(DataPortalEventArgs e, Except
511511
#region Serialization Notification
512512

513513
void ISerializationNotification.Deserialized()
514-
{
515-
OnDeserializedHandler(new System.Runtime.Serialization.StreamingContext());
516-
}
517-
518-
[System.Runtime.Serialization.OnDeserialized]
519-
private void OnDeserializedHandler(System.Runtime.Serialization.StreamingContext context)
520514
{
521515
if (_fieldManager != null)
522516
FieldManager.SetPropertyList(GetType());
523517
InitializeBusinessRules();
518+
OnDeserialized();
519+
}
520+
521+
/// <summary>
522+
/// Invoked after the object has been deserialized.
523+
/// </summary>
524+
protected virtual void OnDeserialized()
525+
{
526+
// do nothing by default
524527
}
525528

526529

Source/Csla/Rules/BusinessRules.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,15 +1302,19 @@ protected override void OnSetChildren(SerializationInfo info, MobileFormatter fo
13021302

13031303
void ISerializationNotification.Deserialized()
13041304
{
1305-
OnDeserializedHandler(new System.Runtime.Serialization.StreamingContext());
1305+
SyncRoot = LockFactory.Create();
1306+
OnDeserialized();
13061307
}
13071308

1308-
[System.Runtime.Serialization.OnDeserialized]
1309-
private void OnDeserializedHandler(System.Runtime.Serialization.StreamingContext context)
1309+
/// <summary>
1310+
/// Invoked after the object has been deserialized.
1311+
/// </summary>
1312+
protected virtual void OnDeserialized()
13101313
{
1311-
SyncRoot = LockFactory.Create();
1314+
// do nothing by default
13121315
}
13131316

1317+
13141318
#endregion
13151319

13161320
#region Get All Broken Rules (tree)

0 commit comments

Comments
 (0)