Skip to content

AllowDelete #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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