diff --git a/DDDCommon.sln b/DDDCommon.sln new file mode 100644 index 0000000..a8af7ac --- /dev/null +++ b/DDDCommon.sln @@ -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 diff --git a/Utils/ModelState.cs b/Utils/ModelState.cs new file mode 100644 index 0000000..500374f --- /dev/null +++ b/Utils/ModelState.cs @@ -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 : ValueObject + { + public T Model { get; } + public ModelStates State { get; } + + public ModelState(T model, ModelStates state = ModelStates.None) + { + Model = model; + State = state; + } + + public ModelState NewState(ModelStates newState) + { + return new ModelState(Model, newState); + } + } +} diff --git a/Utils/ModelStateList.cs b/Utils/ModelStateList.cs new file mode 100644 index 0000000..bc26b41 --- /dev/null +++ b/Utils/ModelStateList.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; + +namespace DDDCommon.Utils +{ + public class ModelStateList : IReadOnlyModelStateList + { + public IReadOnlyList Items => _items; + + public IReadOnlyList AddedItems => _addedItems; + + public IReadOnlyList<(T Old, T New)> UpdatedItems => _updatedItems; + + public IReadOnlyList RemovedItems => _removedItems; + + private readonly List _items; + private readonly List _addedItems; + private readonly List<(T Old, T New)> _updatedItems; + private readonly List _removedItems; + + public ModelStateList(List items = null) + { + _items = items ?? new List(); + _addedItems = new List(); + _updatedItems = new List<(T, T)>(); + _removedItems = new List(); + } + + public void SetItems(List 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); + } + } +} \ No newline at end of file