Snippets
Created by
Hakan Lindestaf
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | using System;
using System.Collections.Generic;
using System.Linq;
namespace DataAccess
{
public static class Util
{
public static void MapManyToManyAssociation<T>(
ICollection<T> currentData,
ICollection<T> newData,
Func<T, T, bool> equality,
Action<T> insert,
Action<T> delete)
{
foreach (var existing in currentData.ToList())
{
var anyMatch = newData.Any(a => equality(existing, a));
if (!anyMatch)
// Remove item
delete(existing);
}
foreach (var newItem in newData)
{
var anyMatch = currentData.Any(a => equality(newItem, a));
if (!anyMatch)
insert(newItem);
}
}
/// <summary>
/// Map the new data to the existing. Changes are handled as a delete/insert.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="currentData"></param>
/// <param name="newData"></param>
/// <param name="equality"></param>
/// <param name="insert"></param>
/// <param name="delete"></param>
/// <param name="update"></param>
public static void MapImmutableOneToMany<TCurrent, TNew>(
ICollection<TCurrent> currentData,
ICollection<TNew> newData,
Func<TCurrent, TNew, bool> equality,
Action<TNew> insert,
Action<TCurrent> delete,
Action<TCurrent, TNew> update = null)
{
var currentItemsToRemove = new List<TCurrent>();
var newDataList = newData?.ToList() ?? new List<TNew>();
foreach (var current in currentData.ToList())
{
var matchingNewEntry = newDataList.FirstOrDefault(a => equality(current, a));
if (matchingNewEntry != null)
{
newDataList.Remove(matchingNewEntry);
if (update != null)
update(current, matchingNewEntry);
}
else
delete(current);
}
// Insert new
newDataList.ForEach(a => insert(a));
}
/// <summary>
/// Map objects based on position in the lists
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="currentData"></param>
/// <param name="newData"></param>
/// <param name="assignment"></param>
/// <param name="insert"></param>
/// <param name="delete"></param>
public static void SequentialOneToManyMap<T>(
ICollection<T> currentData,
ICollection<T> newData,
Action<T, T> assignment,
Action<T> insert,
Action<T> delete)
{
var currentDataArray = currentData.ToArray();
var newDataArray = newData.ToArray();
for (int i = 0; i < currentData.Count; i++)
{
if (i < newData.Count)
// Replace
assignment(currentDataArray[i], newDataArray[i]);
}
if (currentData.Count < newData.Count)
{
// Insert new items
newDataArray.Skip(currentData.Count).ToList().ForEach(a => insert(a));
}
else if (currentData.Count > newData.Count)
{
// Delete items
currentData.Skip(newData.Count).ToList().ForEach(a => delete(a));
}
}
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.