Skip to content

Commit 8ff568e

Browse files
Merge pull request #178 from Lempireqc/master
AllowDelete
2 parents a3e7b13 + b6452fa commit 8ff568e

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This code is provided as is with no warranty. If you find a bug please report it on github.
33
* If you would like to use the code please leave this comment at the top of the page
44
* License MIT (c) Brent McKendrick 2012
@@ -12,23 +12,27 @@
1212

1313
namespace RefactorThis.GraphDiff
1414
{
15-
public static class DbContextExtensions
16-
{
15+
public static class DbContextExtensions
16+
{
1717
/// <summary>
18-
/// Merges a graph of entities with the data store.
18+
/// Merges a graph of entities with the data store.
1919
/// </summary>
2020
/// <typeparam name="T">The type of the root entity</typeparam>
2121
/// <param name="context">The database context to attach / detach.</param>
2222
/// <param name="entity">The root entity.</param>
2323
/// <param name="mapping">The mapping configuration to define the bounds of the graph</param>
24+
/// <param name="allowDelete">NEED TEXTE!!!!</param>
2425
/// <returns>The attached entity graph</returns>
25-
public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new()
26-
{
26+
public static T UpdateGraph<T>(this DbContext context, T entity,
27+
Expression<Func<IUpdateConfiguration<T>, object>> mapping = null, bool allowDelete = true)
28+
where T : class, new()
29+
{
2730
var root = mapping == null ? new GraphNode() : new ConfigurationVisitor<T>().GetNodes(mapping);
31+
root.AllowDelete = allowDelete;
2832
var graphDiffer = new GraphDiffer<T>(root);
2933
return graphDiffer.Merge(context, entity);
30-
}
34+
}
3135

3236
// TODO add IEnumerable<T> entities
33-
}
37+
}
3438
}

GraphDiff/GraphDiff/Internal/Graph/CollectionGraphNode.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,29 @@ public override void Update<T>(DbContext context, T existing, T entity)
4646
}
4747
}
4848

49-
// remove obsolete items
50-
foreach (var dbItem in dbHash.Values)
49+
if (CheckAllowDelete(this))
5150
{
52-
RemoveElement(context, dbItem, dbCollection);
51+
// remove obsolete items
52+
foreach (var dbItem in dbHash.Values)
53+
{
54+
RemoveElement(context, dbItem, dbCollection);
55+
}
56+
}
57+
}
58+
59+
private static bool CheckAllowDelete(GraphNode node)
60+
{
61+
if (node.AllowDelete.HasValue)
62+
{
63+
return node.AllowDelete.Value;
64+
}
65+
else if (node.Parent != null)
66+
{
67+
return CheckAllowDelete(node.Parent);
68+
}
69+
else
70+
{
71+
return true;
5372
}
5473
}
5574

GraphDiff/GraphDiff/Internal/Graph/GraphNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal class GraphNode
1616

1717
public GraphNode Parent { get; private set; }
1818
public Stack<GraphNode> Members { get; private set; }
19+
public bool? AllowDelete { get; set; }
1920

2021
protected readonly PropertyInfo Accessor;
2122

0 commit comments

Comments
 (0)