Skip to content

Commit

Permalink
Merge pull request #178 from Lempireqc/master
Browse files Browse the repository at this point in the history
AllowDelete
  • Loading branch information
JonathanMagnan authored Oct 8, 2018
2 parents a3e7b13 + b6452fa commit 8ff568e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
20 changes: 12 additions & 8 deletions GraphDiff/GraphDiff/DbContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* This code is provided as is with no warranty. If you find a bug please report it on github.
* If you would like to use the code please leave this comment at the top of the page
* License MIT (c) Brent McKendrick 2012
Expand All @@ -12,23 +12,27 @@

namespace RefactorThis.GraphDiff
{
public static class DbContextExtensions
{
public static class DbContextExtensions
{
/// <summary>
/// Merges a graph of entities with the data store.
/// Merges a graph of entities with the data store.
/// </summary>
/// <typeparam name="T">The type of the root entity</typeparam>
/// <param name="context">The database context to attach / detach.</param>
/// <param name="entity">The root entity.</param>
/// <param name="mapping">The mapping configuration to define the bounds of the graph</param>
/// <param name="allowDelete">NEED TEXTE!!!!</param>
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new()
{
public static T UpdateGraph<T>(this DbContext context, T entity,
Expression<Func<IUpdateConfiguration<T>, object>> mapping = null, bool allowDelete = true)
where T : class, new()
{
var root = mapping == null ? new GraphNode() : new ConfigurationVisitor<T>().GetNodes(mapping);
root.AllowDelete = allowDelete;
var graphDiffer = new GraphDiffer<T>(root);
return graphDiffer.Merge(context, entity);
}
}

// TODO add IEnumerable<T> entities
}
}
}
25 changes: 22 additions & 3 deletions GraphDiff/GraphDiff/Internal/Graph/CollectionGraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,29 @@ public override void Update<T>(DbContext context, T existing, T entity)
}
}

// remove obsolete items
foreach (var dbItem in dbHash.Values)
if (CheckAllowDelete(this))
{
RemoveElement(context, dbItem, dbCollection);
// remove obsolete items
foreach (var dbItem in dbHash.Values)
{
RemoveElement(context, dbItem, dbCollection);
}
}
}

private static bool CheckAllowDelete(GraphNode node)
{
if (node.AllowDelete.HasValue)
{
return node.AllowDelete.Value;
}
else if (node.Parent != null)
{
return CheckAllowDelete(node.Parent);
}
else
{
return true;
}
}

Expand Down
1 change: 1 addition & 0 deletions GraphDiff/GraphDiff/Internal/Graph/GraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class GraphNode

public GraphNode Parent { get; private set; }
public Stack<GraphNode> Members { get; private set; }
public bool? AllowDelete { get; set; }

protected readonly PropertyInfo Accessor;

Expand Down

0 comments on commit 8ff568e

Please sign in to comment.