Skip to content

Commit 85c386f

Browse files
committed
Prevent MarkClean() or IsClean() if Topic.IsNew
A `Topic` instance that hasn't been saved can _only_ be dirty, by definition. As such, any efforts to mark a collection associated with a new `Topic` as clean, or to modify the collection with an `!markDirty` parameter should not be honored. There's no need to check this condition from `IsDirty`; instead, we can just make sure it's checked during any entry points, such as e.g. `MarkClean()`, `SetValue()` or `SetTopic()`, `InsertItem()`, and/or `SetItem()`.
1 parent b01216e commit 85c386f

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

OnTopic/Associations/TopicRelationshipMultiMap.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ internal void SetTopic(string relationshipKey, Topic topic, bool? markDirty, boo
187187
var wasDirty = _dirtyKeys.IsDirty(relationshipKey);
188188
if (!topics.Contains(topic)) {
189189
_storage.Add(relationshipKey, topic);
190-
if (markDirty.HasValue && !markDirty.Value && !wasDirty) {
190+
if (!_parent.IsNew && markDirty.HasValue && !markDirty.Value && !wasDirty) {
191191
MarkClean(relationshipKey);
192192
}
193193
else {
@@ -245,10 +245,20 @@ internal void SetTopic(string relationshipKey, Topic topic, bool? markDirty, boo
245245
| METHOD: MARK CLEAN
246246
\-------------------------------------------------------------------------------------------------------------------------*/
247247
/// <inheritdoc/>
248-
public void MarkClean() => _dirtyKeys.MarkClean();
248+
public void MarkClean() {
249+
if (_parent.IsNew) {
250+
return;
251+
}
252+
_dirtyKeys.MarkClean();
253+
}
249254

250255
/// <inheritdoc/>
251-
public void MarkClean(string key) => _dirtyKeys.MarkClean(key);
256+
public void MarkClean(string key) {
257+
if (_parent.IsNew) {
258+
return;
259+
}
260+
_dirtyKeys.MarkClean(key);
261+
}
252262

253263
} //Class
254264
} //Namespace

OnTopic/Collections/Specialized/TrackedCollection{TItem,TValue,TAttribute}.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public bool IsDirty(string key) {
146146
/// cref="Topic.VersionHistory"/>.
147147
/// </param>
148148
public void MarkClean(DateTime? version) {
149+
if (AssociatedTopic.IsNew) {
150+
return;
151+
}
149152
foreach (var trackedItem in Items.Where(a => a.IsDirty).ToArray()) {
150153
SetValue(trackedItem.Key, trackedItem.Value, false, false, version?? DateTime.UtcNow);
151154
}
@@ -169,7 +172,10 @@ public void MarkClean(DateTime? version) {
169172
/// cref="Topic.VersionHistory"/>.
170173
/// </param>
171174
public void MarkClean(string key, DateTime? version) {
172-
if (Contains(key)) {
175+
if (AssociatedTopic.IsNew) {
176+
return;
177+
}
178+
else if (Contains(key)) {
173179
var trackedItem = this[key];
174180
if (trackedItem.IsDirty) {
175181
SetValue(trackedItem.Key, trackedItem.Value, false, false, version?? DateTime.UtcNow);
@@ -444,7 +450,10 @@ internal void SetValue(
444450
\-----------------------------------------------------------------------------------------------------------------------*/
445451
else if (originalItem is not null) {
446452
var markAsDirty = originalItem.IsDirty;
447-
if (markDirty.HasValue) {
453+
if (AssociatedTopic.IsNew) {
454+
markAsDirty = true;
455+
}
456+
else if (markDirty.HasValue) {
448457
markAsDirty = markDirty.Value;
449458
}
450459
else if (originalItem.Value != value) {
@@ -474,7 +483,7 @@ internal void SetValue(
474483
updatedItem = new TItem() {
475484
Key = key,
476485
Value = value,
477-
IsDirty = markDirty ?? true,
486+
IsDirty = AssociatedTopic.IsNew || (markDirty ?? true),
478487
LastModified = version?? DateTime.UtcNow
479488
};
480489
}
@@ -534,6 +543,11 @@ internal void SetValue(
534543
/// </exception>
535544
protected override void InsertItem(int index, TItem item) {
536545
Contract.Requires(item, nameof(item));
546+
if (AssociatedTopic.IsNew && !item.IsDirty) {
547+
item = item with {
548+
IsDirty = true
549+
};
550+
}
537551
if (_topicPropertyDispatcher.Enforce(item.Key, item)) {
538552
if (!Contains(item.Key)) {
539553
base.InsertItem(index, item);
@@ -567,6 +581,11 @@ protected override void InsertItem(int index, TItem item) {
567581
/// <param name="item">The <see cref="TrackedItem{T}"/> object which is being inserted.</param>
568582
protected override void SetItem(int index, TItem item) {
569583
Contract.Requires(item, nameof(item));
584+
if (AssociatedTopic.IsNew && !item.IsDirty) {
585+
item = item with {
586+
IsDirty = true
587+
};
588+
}
570589
if (_topicPropertyDispatcher.Enforce(item.Key, item)) {
571590
base.SetItem(index, item);
572591
if (DeletedItems.Contains(item.Key)) {
@@ -588,7 +607,9 @@ protected override void SetItem(int index, TItem item) {
588607
/// </remarks>
589608
protected override void RemoveItem(int index) {
590609
var trackedItem = this[index];
591-
DeletedItems.Add(trackedItem.Key);
610+
if (!AssociatedTopic.IsNew) {
611+
DeletedItems.Add(trackedItem.Key);
612+
}
592613
base.RemoveItem(index);
593614
}
594615

@@ -604,7 +625,9 @@ protected override void RemoveItem(int index) {
604625
/// cref="TrackedItem{T}"/>s are marked as <see cref="TrackedItem{T}.IsDirty"/>.
605626
/// </remarks>
606627
protected override void ClearItems() {
607-
DeletedItems.AddRange(Items.Select(a => a.Key));
628+
if (!AssociatedTopic.IsNew) {
629+
DeletedItems.AddRange(Items.Select(a => a.Key));
630+
}
608631
base.ClearItems();
609632
}
610633

0 commit comments

Comments
 (0)