Skip to content

Commit 3d1ad6b

Browse files
committed
Ensure that null values are removed entirely
Previously, when a null value was submitted to `SetValue()`, it would simply overwrite the `TrackedRecord<T>.Value` with `null`, but keep the record in place. That is confusing when e.g. calling `Remove()`, and can lead to bugs when relying on e.g. `Contains()`, as records will exist for values that have effectively been deleted. Since we already track these deleted records via `DeletedItems`, it makes more sense to actually remove them. To facilitate this, the null check now explicitly calls `Remove()` instead of falling back to persisting the `TrackedRecord<T>` with the null `Value`. As part of that, I refactored how null values are handled when an existing record doesn't exist; in that case, the method returns quickly, thus preventing the need to check for null `updatedItem` values, or worrying about the `enforceBusinessLogic` being inappropriately disabled. This satisfies the first portion of #79 by ensuring that `SetValue()` actually removes the record. In subsequent commits, I will ensure that `Remove()` and `Clear()` properly enforce business logic.
1 parent 7df6472 commit 3d1ad6b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,11 @@ internal void SetValue(
472472
| Ignore if null
473473
>-------------------------------------------------------------------------------------------------------------------------
474474
| ###NOTE JJC20200501: Null or empty values are treated as deletions, and are not persisted to the data store. With
475-
| existing values, these are written to ensure that the collection is marked as IsDirty, thus allowing previous values to
476-
| be overwritten. Non-existent values, however, should simply be ignored.
475+
| existing values, these are written to DeletedItems to ensure the collection is marked as IsDirty, thus allowing previous
476+
| values to be overwritten. Non-existent values should simply be ignored, however; we shouldn't delete what doesn't exist.
477477
\-----------------------------------------------------------------------------------------------------------------------*/
478478
else if (value is null || String.IsNullOrEmpty(value.ToString())) {
479+
return;
479480
}
480481

481482
/*------------------------------------------------------------------------------------------------------------------------
@@ -507,8 +508,8 @@ internal void SetValue(
507508
/*------------------------------------------------------------------------------------------------------------------------
508509
| Persist item to collection
509510
\-----------------------------------------------------------------------------------------------------------------------*/
510-
if (updatedItem is null) {
511-
return;
511+
if (updatedItem.Value is null) {
512+
Remove(key);
512513
}
513514
else if (originalItem is not null) {
514515
this[IndexOf(originalItem)] = updatedItem;

0 commit comments

Comments
 (0)