-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.28803.452 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DDDCommon", "DDDCommon.csproj", "{9A34CDC8-F596-48CA-B72C-9094629C1CC9}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9A34CDC8-F596-48CA-B72C-9094629C1CC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{9A34CDC8-F596-48CA-B72C-9094629C1CC9}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{9A34CDC8-F596-48CA-B72C-9094629C1CC9}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{9A34CDC8-F596-48CA-B72C-9094629C1CC9}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {8DEA090C-39BC-4155-A46B-7F71B0252C26} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using DDDCommon.Domain.Types; | ||
|
||
namespace DDDCommon.Utils | ||
{ | ||
public enum ModelStates | ||
{ | ||
None = 0, | ||
Added = 1, | ||
Updated = 2, | ||
Removed = 3 | ||
} | ||
public class ModelState<T> : ValueObject | ||
{ | ||
public T Model { get; } | ||
public ModelStates State { get; } | ||
|
||
public ModelState(T model, ModelStates state = ModelStates.None) | ||
{ | ||
Model = model; | ||
State = state; | ||
} | ||
|
||
public ModelState<T> NewState(ModelStates newState) | ||
{ | ||
return new ModelState<T>(Model, newState); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace DDDCommon.Utils | ||
{ | ||
public class ModelStateList<T> : IReadOnlyModelStateList<T> | ||
{ | ||
public IReadOnlyList<T> Items => _items; | ||
|
||
public IReadOnlyList<T> AddedItems => _addedItems; | ||
|
||
public IReadOnlyList<(T Old, T New)> UpdatedItems => _updatedItems; | ||
|
||
public IReadOnlyList<T> RemovedItems => _removedItems; | ||
|
||
private readonly List<T> _items; | ||
private readonly List<T> _addedItems; | ||
private readonly List<(T Old, T New)> _updatedItems; | ||
private readonly List<T> _removedItems; | ||
|
||
public ModelStateList(List<T> items = null) | ||
{ | ||
_items = items ?? new List<T>(); | ||
_addedItems = new List<T>(); | ||
_updatedItems = new List<(T, T)>(); | ||
_removedItems = new List<T>(); | ||
} | ||
|
||
public void SetItems(List<T> items) | ||
{ | ||
ClearAll(); | ||
if (items != null) _items.AddRange(items); | ||
} | ||
|
||
private void ClearAll() | ||
{ | ||
_items.Clear(); | ||
_addedItems.Clear(); | ||
_updatedItems.Clear(); | ||
_removedItems.Clear(); | ||
} | ||
|
||
public void AddItem(T item) | ||
{ | ||
_items.Add(item); | ||
_addedItems.Add(item); | ||
} | ||
|
||
public void UpdateItem(T oldItem, T newItem) | ||
{ | ||
var oldItemIndex = _items.IndexOf(oldItem); | ||
if (oldItemIndex < 0) throw Errors.NotFound(); | ||
|
||
_items[oldItemIndex] = newItem; | ||
_updatedItems.Add((oldItem, newItem)); | ||
} | ||
|
||
public void RemoveItem(T item) | ||
{ | ||
if (_items.Remove(item)) _removedItems.Add(item); | ||
} | ||
} | ||
} |