Skip to content

Commit

Permalink
add 'ModelStateList' type
Browse files Browse the repository at this point in the history
  • Loading branch information
hatami57 committed May 19, 2019
1 parent d26ce97 commit 03ed276
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DDDCommon.sln
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
28 changes: 28 additions & 0 deletions Utils/ModelState.cs
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);
}
}
}
63 changes: 63 additions & 0 deletions Utils/ModelStateList.cs
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);
}
}
}

0 comments on commit 03ed276

Please sign in to comment.