Skip to content

Commit

Permalink
Extract INavigationBase
Browse files Browse the repository at this point in the history
Part of #19003
  • Loading branch information
AndriySvyryd committed Jan 8, 2020
1 parent af81c0c commit 4498374
Show file tree
Hide file tree
Showing 86 changed files with 795 additions and 594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class CosmosNavigationExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static bool IsEmbedded([NotNull] this INavigation navigation)
=> !navigation.IsDependentToPrincipal()
=> !navigation.IsOnDependent
&& !navigation.ForeignKey.DeclaringEntityType.IsDocumentRoot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ protected override Expression VisitMember(MemberExpression memberExpression)
{
case EntityProjectionExpression entityProjection:
return new EntityShaperExpression(
navigation.GetTargetType(),
navigation.TargetEntityType,
Expression.Convert(Expression.Convert(entityProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);

case ObjectArrayProjectionExpression objectArrayProjectionExpression:
{
var innerShaperExpression = new EntityShaperExpression(
navigation.GetTargetType(),
navigation.TargetEntityType,
Expression.Convert(
Expression.Convert(objectArrayProjectionExpression.InnerProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);
Expand Down Expand Up @@ -343,14 +343,14 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
{
case EntityProjectionExpression entityProjection:
return new EntityShaperExpression(
navigation.GetTargetType(),
navigation.TargetEntityType,
Expression.Convert(Expression.Convert(entityProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);

case ObjectArrayProjectionExpression objectArrayProjectionExpression:
{
var innerShaperExpression = new EntityShaperExpression(
navigation.GetTargetType(),
navigation.TargetEntityType,
Expression.Convert(
Expression.Convert(objectArrayProjectionExpression.InnerProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
var lambda = (LambdaExpression)methodCallExpression.Arguments[1];
if (lambda.Body is IncludeExpression includeExpression)
{
if (includeExpression.Navigation.IsDependentToPrincipal()
if (includeExpression.Navigation.IsOnDependent
|| includeExpression.Navigation.ForeignKey.DeclaringEntityType.IsDocumentRoot())
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -285,14 +285,14 @@ protected override Expression VisitExtension(Expression extensionExpression)

var navigation = collectionShaperExpression.Navigation;
return Expression.Call(
_populateCollectionMethodInfo.MakeGenericMethod(navigation.GetTargetType().ClrType, navigation.ClrType),
_populateCollectionMethodInfo.MakeGenericMethod(navigation.TargetEntityType.ClrType, navigation.ClrType),
Expression.Constant(navigation.GetCollectionAccessor()),
entities);
}

case IncludeExpression includeExpression:
{
if (includeExpression.Navigation.IsDependentToPrincipal()
if (includeExpression.Navigation.IsOnDependent
|| includeExpression.Navigation.ForeignKey.DeclaringEntityType.IsDocumentRoot())
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -359,14 +359,14 @@ private void AddInclude(
Expression instanceVariable)
{
var navigation = includeExpression.Navigation;
var includeMethod = navigation.IsCollection() ? _includeCollectionMethodInfo : _includeReferenceMethodInfo;
var includeMethod = navigation.IsCollection ? _includeCollectionMethodInfo : _includeReferenceMethodInfo;
var includingClrType = navigation.DeclaringEntityType.ClrType;
var relatedEntityClrType = navigation.GetTargetType().ClrType;
var relatedEntityClrType = navigation.TargetEntityType.ClrType;
var entityEntryVariable = _trackQueryResults
? shaperBlock.Variables.Single(v => v.Type == typeof(InternalEntityEntry))
: (Expression)Expression.Constant(null, typeof(InternalEntityEntry));
var concreteEntityTypeVariable = shaperBlock.Variables.Single(v => v.Type == typeof(IEntityType));
var inverseNavigation = navigation.FindInverse();
var inverseNavigation = navigation.Inverse;
var fixup = GenerateFixup(
includingClrType, relatedEntityClrType, navigation, inverseNavigation);
var initialize = GenerateInitialize(includingClrType, navigation);
Expand Down Expand Up @@ -398,7 +398,7 @@ private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
INavigation navigation,
INavigation inverseNavigation,
Action<TIncludingEntity, TIncludedEntity> fixup,
Action<TIncludingEntity> initialize)
Action<TIncludingEntity> _)
{
if (entity == null
|| !navigation.DeclaringEntityType.IsAssignableFrom(entityType))
Expand All @@ -414,7 +414,7 @@ private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
{
fixup(includingEntity, relatedEntity);
if (inverseNavigation != null
&& !inverseNavigation.IsCollection())
&& !inverseNavigation.IsCollection)
{
SetIsLoadedNoTracking(relatedEntity, inverseNavigation);
}
Expand Down Expand Up @@ -503,15 +503,15 @@ private static Delegate GenerateFixup(
var relatedEntityParameter = Expression.Parameter(relatedEntityType);
var expressions = new List<Expression>
{
navigation.IsCollection()
navigation.IsCollection
? AddToCollectionNavigation(entityParameter, relatedEntityParameter, navigation)
: AssignReferenceNavigation(entityParameter, relatedEntityParameter, navigation)
};

if (inverseNavigation != null)
{
expressions.Add(
inverseNavigation.IsCollection()
inverseNavigation.IsCollection
? AddToCollectionNavigation(relatedEntityParameter, entityParameter, inverseNavigation)
: AssignReferenceNavigation(relatedEntityParameter, entityParameter, inverseNavigation));
}
Expand All @@ -524,7 +524,7 @@ private static Delegate GenerateInitialize(
Type entityType,
INavigation navigation)
{
if (!navigation.IsCollection())
if (!navigation.IsCollection)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ public virtual Expression BindNavigation([NotNull] INavigation navigation, bool

if (!_navigationExpressionsCache.TryGetValue(navigation, out var expression))
{
if (navigation.IsCollection())
if (navigation.IsCollection)
{
expression = new ObjectArrayProjectionExpression(navigation, AccessExpression);
}
else
{
expression = new EntityProjectionExpression(
navigation.GetTargetType(),
navigation.TargetEntityType,
new ObjectAccessExpression(navigation, AccessExpression));
}

Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Query/Internal/ObjectAccessExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ObjectAccessExpression : Expression, IPrintableExpression, IAccessE
/// </summary>
public ObjectAccessExpression([NotNull] INavigation navigation, [NotNull] Expression accessExpression)
{
Name = navigation.GetTargetType().GetContainingPropertyName();
Name = navigation.TargetEntityType.GetContainingPropertyName();
if (Name == null)
{
throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ObjectArrayProjectionExpression(
[NotNull] Expression accessExpression,
[CanBeNull] EntityProjectionExpression innerProjection = null)
{
var targetType = navigation.GetTargetType();
var targetType = navigation.TargetEntityType;
Type = typeof(IEnumerable<>).MakeGenericType(targetType.ClrType);

Name = targetType.GetContainingPropertyName();
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public virtual JObject CreateDocument([NotNull] IUpdateEntry entry, int? ordinal
{
var fk = embeddedNavigation.ForeignKey;
if (!fk.IsOwnership
|| embeddedNavigation.IsDependentToPrincipal()
|| embeddedNavigation.IsOnDependent
|| fk.DeclaringEntityType.IsDocumentRoot())
{
continue;
Expand Down Expand Up @@ -177,7 +177,7 @@ public virtual JObject UpdateDocument([NotNull] JObject document, [NotNull] IUpd
{
var fk = ownedNavigation.ForeignKey;
if (!fk.IsOwnership
|| ownedNavigation.IsDependentToPrincipal()
|| ownedNavigation.IsOnDependent
|| fk.DeclaringEntityType.IsDocumentRoot())
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected virtual void GenerateConstructor(
{
Check.NotNull(entityType, nameof(entityType));

var collectionNavigations = entityType.GetNavigations().Where(n => n.IsCollection()).ToList();
var collectionNavigations = entityType.GetNavigations().Where(n => n.IsCollection).ToList();

if (collectionNavigations.Count > 0)
{
Expand All @@ -182,7 +182,7 @@ protected virtual void GenerateConstructor(
{
foreach (var navigation in collectionNavigations)
{
_sb.AppendLine($"{navigation.Name} = new HashSet<{navigation.GetTargetType().Name}>();");
_sb.AppendLine($"{navigation.Name} = new HashSet<{navigation.TargetEntityType.Name}>();");
}
}

Expand Down Expand Up @@ -304,8 +304,8 @@ protected virtual void GenerateNavigationProperties(
Check.NotNull(entityType, nameof(entityType));

var sortedNavigations = entityType.GetNavigations()
.OrderBy(n => n.IsDependentToPrincipal() ? 0 : 1)
.ThenBy(n => n.IsCollection() ? 1 : 0)
.OrderBy(n => n.IsOnDependent ? 0 : 1)
.ThenBy(n => n.IsCollection ? 1 : 0)
.ToList();

if (sortedNavigations.Any())
Expand All @@ -319,8 +319,8 @@ protected virtual void GenerateNavigationProperties(
GenerateNavigationDataAnnotations(navigation);
}

var referencedTypeName = navigation.GetTargetType().Name;
var navigationType = navigation.IsCollection() ? $"ICollection<{referencedTypeName}>" : referencedTypeName;
var referencedTypeName = navigation.TargetEntityType.Name;
var navigationType = navigation.IsCollection ? $"ICollection<{referencedTypeName}>" : referencedTypeName;
_sb.AppendLine($"public virtual {navigationType} {navigation.Name} {{ get; set; }}");
}
}
Expand All @@ -334,7 +334,7 @@ private void GenerateNavigationDataAnnotations(INavigation navigation)

private void GenerateForeignKeyAttribute(INavigation navigation)
{
if (navigation.IsDependentToPrincipal())
if (navigation.IsOnDependent)
{
if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
{
Expand All @@ -360,7 +360,7 @@ private void GenerateInversePropertyAttribute(INavigation navigation)
{
if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
{
var inverseNavigation = navigation.FindInverse();
var inverseNavigation = navigation.Inverse;

if (inverseNavigation != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ private Expression TryExpand(Expression source, MemberIdentity member)
return null;
}

var targetEntityType = navigation.GetTargetType();
var targetEntityType = navigation.TargetEntityType;
if (targetEntityType == null
|| (!targetEntityType.HasDefiningNavigation()
&& !targetEntityType.IsOwned()))
Expand All @@ -1031,7 +1031,7 @@ private Expression TryExpand(Expression source, MemberIdentity member)
}

var foreignKey = navigation.ForeignKey;
if (navigation.IsCollection())
if (navigation.IsCollection)
{
var innerShapedQuery = CreateShapedQueryExpression(targetEntityType);
var innerQueryExpression = (InMemoryQueryExpression)innerShapedQuery.QueryExpression;
Expand All @@ -1042,12 +1042,12 @@ private Expression TryExpand(Expression source, MemberIdentity member)
.Any(t => t.IsNullableType());

var outerKey = entityShaperExpression.CreateKeyAccessExpression(
navigation.IsDependentToPrincipal()
navigation.IsOnDependent
? foreignKey.Properties
: foreignKey.PrincipalKey.Properties,
makeNullable);
var innerKey = innerShapedQuery.ShaperExpression.CreateKeyAccessExpression(
navigation.IsDependentToPrincipal()
navigation.IsOnDependent
? foreignKey.PrincipalKey.Properties
: foreignKey.Properties,
makeNullable);
Expand Down Expand Up @@ -1090,12 +1090,12 @@ ProjectionBindingExpression projectionBindingExpression
.Any(t => t.IsNullableType());

var outerKey = entityShaperExpression.CreateKeyAccessExpression(
navigation.IsDependentToPrincipal()
navigation.IsOnDependent
? foreignKey.Properties
: foreignKey.PrincipalKey.Properties,
makeNullable);
var innerKey = innerShapedQuery.ShaperExpression.CreateKeyAccessExpression(
navigation.IsDependentToPrincipal()
navigation.IsOnDependent
? foreignKey.PrincipalKey.Properties
: foreignKey.Properties,
makeNullable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static void IncludeReference<TEntity, TIncludingEntity, TIncludedEntity>
{
fixup(includingEntity, relatedEntity);
if (inverseNavigation != null
&& !inverseNavigation.IsCollection())
&& !inverseNavigation.IsCollection)
{
SetIsLoadedNoTracking(relatedEntity, inverseNavigation);
}
Expand Down Expand Up @@ -165,15 +165,15 @@ protected override Expression VisitExtension(Expression extensionExpression)
{
var entityClrType = includeExpression.EntityExpression.Type;
var includingClrType = includeExpression.Navigation.DeclaringEntityType.ClrType;
var inverseNavigation = includeExpression.Navigation.FindInverse();
var relatedEntityClrType = includeExpression.Navigation.GetTargetType().ClrType;
var inverseNavigation = includeExpression.Navigation.Inverse;
var relatedEntityClrType = includeExpression.Navigation.TargetEntityType.ClrType;
if (includingClrType != entityClrType
&& includingClrType.IsAssignableFrom(entityClrType))
{
includingClrType = entityClrType;
}

if (includeExpression.Navigation.IsCollection())
if (includeExpression.Navigation.IsCollection)
{
var collectionShaper = (CollectionShaperExpression)includeExpression.NavigationExpression;
return Expression.Call(
Expand Down Expand Up @@ -240,15 +240,15 @@ private static LambdaExpression GenerateFixup(
var relatedEntityParameter = Expression.Parameter(relatedEntityType);
var expressions = new List<Expression>
{
navigation.IsCollection()
navigation.IsCollection
? AddToCollectionNavigation(entityParameter, relatedEntityParameter, navigation)
: AssignReferenceNavigation(entityParameter, relatedEntityParameter, navigation)
};

if (inverseNavigation != null)
{
expressions.Add(
inverseNavigation.IsCollection()
inverseNavigation.IsCollection
? AddToCollectionNavigation(relatedEntityParameter, entityParameter, inverseNavigation)
: AssignReferenceNavigation(relatedEntityParameter, entityParameter, inverseNavigation));
}
Expand Down
Loading

0 comments on commit 4498374

Please sign in to comment.