Skip to content

Commit a3723d8

Browse files
Code Review
1 parent 5031a22 commit a3723d8

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

GraphDiff/GraphDiff.Shared/IUpdateConfiguration.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ public static IUpdateConfiguration<T> AssociatedCollection<T, T2>(this IUpdateCo
8989
return config;
9090
}
9191

92-
// NEED TEXT!
92+
/// <summary>
93+
/// States that the child collection is not a part of the aggregate. The parent's navigation property will be updated, but entity changes to the
94+
/// child entities will not be saved.
95+
/// </summary>
96+
/// <typeparam name="T">The parent entity type</typeparam>
97+
/// <typeparam name="T2">The child entity type </typeparam>
98+
/// <param name="config">The configuration mapping</param>
99+
/// <param name="expression">An expression specifying the child entity</param>
100+
/// <param name="navExpression">An navigation expression specifying the parent entity</param>
101+
/// <returns>Updated configuration mapping</returns>
93102
public static IUpdateConfiguration<T> AssociatedCollection<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, ICollection<T2>>> expression, Expression<Func<T2, T>> navExpression)
94103
{
95104
return config;

GraphDiff/GraphDiff.Shared/Internal/ConfigurationVisitor.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class ConfigurationVisitor<T> : ExpressionVisitor
1313
{
1414
private GraphNode _currentMember;
1515
private string _currentMethod = "";
16-
private bool isAssociatedCollectionWithNav = false;
16+
private bool _isAssociatedCollectionWithNav;
1717

1818
public GraphNode GetNodes(Expression<Func<IUpdateConfiguration<T>, object>> expression)
1919
{
@@ -27,19 +27,18 @@ protected override Expression VisitMember(MemberExpression memberExpression)
2727
{
2828
var accessor = GetMemberAccessor(memberExpression);
2929

30-
if (isAssociatedCollectionWithNav)
31-
{
30+
if (_isAssociatedCollectionWithNav)
31+
{
3232
_currentMember.AccessorCyclicNavigationProperty = accessor;
3333
}
34-
else
35-
{
34+
else
35+
{
3636
var newMember = CreateNewMember(accessor);
3737

3838
_currentMember.Members.Push(newMember);
3939
_currentMember = newMember;
4040
}
4141

42-
4342
return base.VisitMember(memberExpression);
4443
}
4544

@@ -50,27 +49,30 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)
5049
if (_currentMethod.Equals("AssociatedCollection") && expression.Arguments.Count == 3)
5150
{
5251
Visit(expression.Arguments[1]);
53-
isAssociatedCollectionWithNav = true;
52+
5453
try
55-
{
54+
{
55+
_isAssociatedCollectionWithNav = true;
5656
Visit(expression.Arguments[2]);
57-
isAssociatedCollectionWithNav = false;
5857
}
59-
catch (Exception e)
60-
{
61-
isAssociatedCollectionWithNav = false;
58+
catch
59+
{
6260
throw;
63-
}
61+
}
62+
finally
63+
{
64+
_isAssociatedCollectionWithNav = false;
65+
}
6466
}
6567
else
66-
{
68+
{
6769
// go left to right in the subtree (ignore first argument for now)
6870
for (int i = 1; i < expression.Arguments.Count; i++)
6971
{
7072
Visit(expression.Arguments[i]);
7173
}
7274
}
73-
75+
7476
// go back up the tree and continue
7577
_currentMember = _currentMember.Parent;
7678
return Visit(expression.Arguments[0]);

GraphDiff/GraphDiff.Shared/Internal/Graph/GraphNode.cs

+7-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class GraphNode
1717
public GraphNode Parent { get; private set; }
1818
public Stack<GraphNode> Members { get; private set; }
1919
public bool? AllowDelete { get; set; }
20-
20+
2121
protected readonly PropertyInfo Accessor;
2222

2323
internal PropertyInfo AccessorCyclicNavigationProperty;
@@ -106,7 +106,7 @@ protected static IEnumerable<string> GetRequiredNavigationPropertyIncludes(DbCon
106106
.Select(navigationProperty => ownIncludeString + "." + navigationProperty.Name);
107107
}
108108

109-
protected static void AttachCyclicNavigationProperty(IObjectContextAdapter context, object parent, object child, PropertyInfo navProperty = null)
109+
protected static void AttachCyclicNavigationProperty(IObjectContextAdapter context, object parent, object child, PropertyInfo parentNavigationProperty = null)
110110
{
111111
if (parent == null || child == null) return;
112112

@@ -115,18 +115,13 @@ protected static void AttachCyclicNavigationProperty(IObjectContextAdapter conte
115115

116116
var navigationProperties = context.GetNavigationPropertiesForType(childType);
117117

118-
PropertyInfo parentNavigationProperty = null;
119-
120-
if (navProperty != null)
118+
if (parentNavigationProperty == null)
121119
{
122-
parentNavigationProperty = navProperty;
123-
}
124-
else
125-
{
120+
// IF not parent property is specified, we take the first one if we found something
126121
parentNavigationProperty = navigationProperties
127-
.Where(navigation => navigation.TypeUsage.EdmType.Name == parentType.Name)
128-
.Select(navigation => childType.GetProperty(navigation.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
129-
.FirstOrDefault();
122+
.Where(navigation => navigation.TypeUsage.EdmType.Name == parentType.Name)
123+
.Select(navigation => childType.GetProperty(navigation.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
124+
.FirstOrDefault();
130125
}
131126

132127
if (parentNavigationProperty != null)

GraphDiff/Z.EntityFrameworkGraphDiff.labEF6/Request_ManyNav.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public static void Execute()
5151
// with.OwnedCollection(p => p.EntitySimples)));
5252
// context.UpdateGraph(entity, map => map.AssociatedEntity(c => c.EntitySimpleChild).AssociatedCollection(c => c.EntitySimpleLists, x => x.B));
5353

54+
//context.UpdateGraph(entity, map => map.AssociatedCollection(c => c.EntitySimpleLists).AssociatedEntity(c => c.EntitySimpleChild));
55+
5456
context.UpdateGraph(entity, map => map.AssociatedCollection(c => c.EntitySimpleLists, x => x.B).AssociatedEntity(c => c.EntitySimpleChild));
55-
57+
5658
//context.UpdateGraph(entity, map => map.AssociatedEntity(c => c.EntitySimpleChild).AssociatedCollection(c => c.EntitySimpleLists));
5759
//context.UpdateGraph(entity, map => map.OwnedEntity(c => c.EntitySimpleChild).OwnedCollection(c => c.EntitySimpleLists));
5860
context.SaveChanges();

0 commit comments

Comments
 (0)