Skip to content
Open
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
83 changes: 55 additions & 28 deletions GraphDiff/GraphDiff/DbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@
namespace RefactorThis.GraphDiff
{
public static class DbContextExtensions
{
{
/// <summary>
/// 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="persisted">The persisted entity, optional, for performance optimisation.</param>
/// <param name="mapping">The mapping configuration to define the bounds of the graph</param>
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, T persisted, Expression<Func<IUpdateConfiguration<T>, object>> mapping) where T : class
{
return UpdateGraph(context, entity, persisted, mapping, null, null);
}

/// <summary>
/// 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>
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping) where T : class
{
return UpdateGraph(context, entity, null, mapping, null, null);
}

/// <summary>
/// Merges a graph of entities with the data store.
/// </summary>
Expand All @@ -25,9 +52,9 @@ public static class DbContextExtensions
/// <param name="updateParams">Update configuration overrides</param>
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping, UpdateParams updateParams = null) where T : class
{
return UpdateGraph(context, entity, mapping, null, updateParams);
}
{
return UpdateGraph(context, entity, null, mapping, null, updateParams);
}

/// <summary>
/// Merges a graph of entities with the data store.
Expand All @@ -40,7 +67,7 @@ public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, string mappingScheme, UpdateParams updateParams = null) where T : class
{
return UpdateGraph(context, entity, null, mappingScheme, updateParams);
return UpdateGraph(context, entity, null, null, mappingScheme, updateParams);
}

/// <summary>
Expand All @@ -53,7 +80,7 @@ public static T UpdateGraph<T>(this DbContext context, T entity, string mappingS
/// <returns>The attached entity graph</returns>
public static T UpdateGraph<T>(this DbContext context, T entity, UpdateParams updateParams = null) where T : class
{
return UpdateGraph(context, entity, null, null, updateParams);
return UpdateGraph(context, entity, null, null, null, updateParams);
}

/// <summary>
Expand All @@ -80,7 +107,7 @@ public static T LoadAggregate<T>(this DbContext context, Expression<Func<T, bool
}

// other methods are convenience wrappers around this.
private static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping,
private static T UpdateGraph<T>(this DbContext context, T entity, T persisted, Expression<Func<IUpdateConfiguration<T>, object>> mapping,
string mappingScheme, UpdateParams updateParams) where T : class
{
if (entity == null)
Expand All @@ -94,28 +121,28 @@ private static T UpdateGraph<T>(this DbContext context, T entity, Expression<Fun
var differ = new GraphDiffer<T>(context, queryLoader, entityManager, root);

var queryMode = updateParams != null ? updateParams.QueryMode : QueryMode.SingleQuery;
return differ.Merge(entity, queryMode);
return differ.Merge(entity, persisted, queryMode);
}

private static GraphNode GetRootNode<T>(Expression<Func<IUpdateConfiguration<T>, object>> mapping, string mappingScheme, AggregateRegister register) where T : class
{
GraphNode root;
if (mapping != null)
{
// mapping configuration
root = register.GetEntityGraph(mapping);
}
else if (mappingScheme != null)
{
// names scheme
root = register.GetEntityGraph<T>(mappingScheme);
}
else
{
// attributes or null
root = register.GetEntityGraph<T>();
}
return root;
}
}
{
GraphNode root;
if (mapping != null)
{
// mapping configuration
root = register.GetEntityGraph(mapping);
}
else if (mappingScheme != null)
{
// names scheme
root = register.GetEntityGraph<T>(mappingScheme);
}
else
{
// attributes or null
root = register.GetEntityGraph<T>();
}
return root;
}
}
}
12 changes: 8 additions & 4 deletions GraphDiff/GraphDiff/Internal/GraphDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace RefactorThis.GraphDiff.Internal
{
internal interface IGraphDiffer<T> where T : class
{
T Merge(T updating, QueryMode queryMode = QueryMode.SingleQuery);
T Merge(T updating, T persisted, QueryMode queryMode = QueryMode.SingleQuery);
}

/// <summary>GraphDiff main entry point.</summary>
Expand All @@ -26,7 +26,7 @@ public GraphDiffer(DbContext dbContext, IQueryLoader queryLoader, IEntityManager
_entityManager = entityManager;
}

public T Merge(T updating, QueryMode queryMode = QueryMode.SingleQuery)
public T Merge(T updating, T persisted, QueryMode queryMode = QueryMode.SingleQuery)
{
// todo query mode
bool isAutoDetectEnabled = _dbContext.Configuration.AutoDetectChangesEnabled;
Expand All @@ -37,7 +37,11 @@ public T Merge(T updating, QueryMode queryMode = QueryMode.SingleQuery)

// Get our entity with all includes needed, or add a new entity
var includeStrings = _root.GetIncludeStrings(_entityManager);
T persisted = _queryLoader.LoadEntity(updating, includeStrings, queryMode);

if (persisted == null)
{
persisted = _queryLoader.LoadEntity(updating, includeStrings, queryMode);
}

if (persisted == null)
{
Expand All @@ -52,7 +56,7 @@ public T Merge(T updating, QueryMode queryMode = QueryMode.SingleQuery)
{
throw new InvalidOperationException(
String.Format("Entity of type '{0}' is already in an attached state. GraphDiff supports detached entities only at this time. Please try AsNoTracking() or detach your entites before calling the UpdateGraph method.",
typeof (T).FullName));
typeof(T).FullName));
}

// Perform recursive update
Expand Down