Skip to content
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

Store IsLoaded state in lazy-loading proxies #14733

Merged
merged 1 commit into from
Feb 28, 2019
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
21 changes: 16 additions & 5 deletions src/EFCore.Abstractions/Infrastructure/ILazyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,20 +15,31 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
/// of loading navigation properties automatically the first time they are accessed.
/// </para>
/// <para>
/// The service lifetime is 'ServiceLifetime.Scoped'. This means that each
/// 'DbContext' instance will use its own instance of this service.
/// The service lifetime is 'ServiceLifetime.Transient'. This means that each
/// entity instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public interface ILazyLoader
public interface ILazyLoader : IDisposable
{
/// <summary>
/// Sets the given navigation as known to be completely loaded or known to be
/// no longer completely loaded.
/// </summary>
/// <param name="entity"> The entity on which the navigation property is located. </param>
/// <param name="navigationName"> The navigation property name. </param>
/// <param name="loaded"> Determines whether the navigation is set as loaded or not. </param>
void SetLoaded(
[NotNull] object entity,
[NotNull] [CallerMemberName] string navigationName = "",
bool loaded = true);

/// <summary>
/// Loads a navigation property if it has not already been loaded.
/// </summary>
/// <param name="entity"> The entity on which the navigation property is located. </param>
/// <param name="navigationName"> The navigation property name. </param>
// ReSharper disable once AssignNullToNotNullAttribute
void Load([NotNull] object entity, [NotNull] [CallerMemberName] string navigationName = "");

/// <summary>
Expand All @@ -42,7 +54,6 @@ Task LoadAsync(
#pragma warning restore CA1068 // CancellationToken parameters must come last
[NotNull] object entity,
CancellationToken cancellationToken = default,
// ReSharper disable once AssignNullToNotNullAttribute
[NotNull] [CallerMemberName] string navigationName = "");
}
}
17 changes: 10 additions & 7 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand Down Expand Up @@ -1045,10 +1046,9 @@ private void SetProperty(

if (propertyBase is INavigation navigation)
{
if (!navigation.IsCollection()
&& value == null)
if (!navigation.IsCollection())
{
SetIsLoaded(navigation, loaded: false);
SetIsLoaded(navigation, value != null);
}
}

Expand Down Expand Up @@ -1473,17 +1473,20 @@ public virtual void SetIsLoaded([NotNull] INavigation navigation, bool loaded =
}

_stateData.FlagProperty(navigation.GetIndex(), PropertyFlag.IsLoaded, isFlagged: loaded);

var lazyLoaderProperty = EntityType.GetServiceProperties().FirstOrDefault(p => p.ClrType == typeof(ILazyLoader));
if (lazyLoaderProperty != null)
{
((ILazyLoader)this[lazyLoaderProperty])?.SetLoaded(Entity, navigation.Name, loaded);
}
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual bool IsLoaded([NotNull] INavigation navigation)
=> (!navigation.IsCollection()
&& EntityState != EntityState.Detached
&& this[navigation] != null)
|| _stateData.IsPropertyFlagged(navigation.GetIndex(), PropertyFlag.IsLoaded);
=> _stateData.IsPropertyFlagged(navigation.GetIndex(), PropertyFlag.IsLoaded);

IUpdateEntry IUpdateEntry.SharedIdentityEntry => SharedIdentityEntry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public static readonly IDictionary<Type, ServiceCharacteristics> CoreServices
{ typeof(IQueryContextFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IEntityQueryableExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IEntityQueryModelVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ILazyLoader), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ReLinq.IEvaluatableExpressionFilter), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IEvaluatableExpressionFilter), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ILazyLoader), new ServiceCharacteristics(ServiceLifetime.Transient) },
{ typeof(IParameterBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(ITypeMappingSourcePlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(ISingletonOptions), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
Expand Down
44 changes: 33 additions & 11 deletions src/EFCore/Internal/LazyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -23,15 +24,16 @@ namespace Microsoft.EntityFrameworkCore.Internal
/// directly from your code. This API may change or be removed in future releases.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped"/>. This means that each
/// <see cref="DbContext"/> instance will use its own instance of this service.
/// The service lifetime is <see cref="ServiceLifetime.Transient"/>. This means that each
/// entity instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public class LazyLoader : ILazyLoader, IDisposable
public class LazyLoader : ILazyLoader
{
private bool _disposed;
private IDictionary<string, bool>? _loadedStates;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand All @@ -48,6 +50,23 @@ public LazyLoader(
Logger = logger;
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void SetLoaded(
object entity,
[CallerMemberName] string navigationName = "",
bool loaded = true)
{
if (_loadedStates == null)
{
_loadedStates = new Dictionary<string, bool>();
}

_loadedStates[navigationName] = loaded;
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
Expand Down Expand Up @@ -94,8 +113,17 @@ public virtual Task LoadAsync(
: Task.CompletedTask;
}

private bool ShouldLoad(object entity, string navigationName, [NotNullWhenTrue] out NavigationEntry? navigationEntry)
private bool ShouldLoad(object entity, string navigationName,
[NotNullWhenTrue] out NavigationEntry? navigationEntry)
{
if (_loadedStates != null
&& _loadedStates.TryGetValue(navigationName, out var loaded)
&& loaded)
{
navigationEntry = null;
return false;
}

if (_disposed)
{
Logger.LazyLoadOnDisposedContextWarning(Context, entity, navigationName);
Expand All @@ -107,13 +135,7 @@ private bool ShouldLoad(object entity, string navigationName, [NotNullWhenTrue]

if (entityEntry.State == EntityState.Detached)
{
var value = tempNavigationEntry.CurrentValue;
if (value == null
|| (tempNavigationEntry.Metadata.IsCollection()
&& !((IEnumerable)value).Any()))
{
Logger.DetachedLazyLoadingWarning(Context, entity, navigationName);
}
Logger.DetachedLazyLoadingWarning(Context, entity, navigationName);
}
else if (!tempNavigationEntry.IsLoaded)
{
Expand Down
79 changes: 66 additions & 13 deletions src/EFCore/Query/Internal/IncludeCompiler.IncludeLoadTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,51 @@ var stateManagerProperty
relatedArrayAccessExpression,
Expression.Constant(Navigation.GetTargetType())));

var navigationExpression = Expression.Constant(Navigation);

blockExpressions.Add(
Expression.Call(
_setRelationshipSnapshotValueMethodInfo,
stateManagerProperty,
Expression.Constant(Navigation),
navigationExpression,
targetEntityExpression,
relatedArrayAccessExpression));

blockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedMethodInfo,
stateManagerProperty,
navigationExpression,
targetEntityExpression));

isNullBlockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedMethodInfo,
stateManagerProperty,
Expression.Constant(Navigation),
navigationExpression,
targetEntityExpression));

}
else
{
blockExpressions.Add(
targetEntityExpression
.MakeMemberAccess(Navigation.GetMemberInfo(false, true))
.CreateAssignExpression(relatedEntityExpression));

var navigationExpression = Expression.Constant(Navigation);

blockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedNoTrackingMethodInfo,
navigationExpression,
targetEntityExpression));

isNullBlockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedNoTrackingMethodInfo,
navigationExpression,
targetEntityExpression));
}

var inverseNavigation = Navigation.FindInverse();
Expand All @@ -398,6 +422,8 @@ var stateManagerProperty
{
var collection = inverseNavigation.IsCollection();

var inverseNavigationExpression = Expression.Constant(inverseNavigation);

if (trackingQuery)
{
blockExpressions.Add(
Expand All @@ -406,22 +432,44 @@ var stateManagerProperty
? _addToCollectionSnapshotMethodInfo
: _setRelationshipSnapshotValueMethodInfo,
stateManagerProperty,
Expression.Constant(inverseNavigation),
inverseNavigationExpression,
relatedArrayAccessExpression,
targetEntityExpression));

if (!collection)
{
blockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedMethodInfo,
stateManagerProperty,
inverseNavigationExpression,
relatedArrayAccessExpression));
}
}
else
{
blockExpressions.Add(
collection
? Expression.Call(
if (collection)
{
blockExpressions.Add(
Expression.Call(
Expression.Constant(inverseNavigation.GetCollectionAccessor()),
_collectionAccessorAddMethodInfo,
relatedArrayAccessExpression,
targetEntityExpression)
: relatedEntityExpression.MakeMemberAccess(
targetEntityExpression));
}
else
{
blockExpressions.Add(
relatedEntityExpression.MakeMemberAccess(
inverseNavigation.GetMemberInfo(forConstruction: false, forSet: true))
.CreateAssignExpression(targetEntityExpression));

blockExpressions.Add(
Expression.Call(
_setRelationshipIsLoadedNoTrackingMethodInfo,
inverseNavigationExpression,
relatedArrayAccessExpression));
}
}
}

Expand Down Expand Up @@ -490,13 +538,18 @@ private static void SetRelationshipIsLoaded(
IStateManager stateManager,
IPropertyBase navigation,
object entity)
{
var internalEntityEntry = stateManager.TryGetEntry(entity);
=> stateManager
.TryGetEntry(entity, (IEntityType)navigation.DeclaringType)
.SetIsLoaded((INavigation)navigation);

Debug.Assert(internalEntityEntry != null);
private static readonly MethodInfo _setRelationshipIsLoadedNoTrackingMethodInfo
= typeof(IncludeLoadTreeNode).GetTypeInfo()
.GetDeclaredMethod(nameof(SetRelationshipIsLoadedNoTracking));

internalEntityEntry.SetIsLoaded((INavigation)navigation);
}
private static void SetRelationshipIsLoadedNoTracking(
IPropertyBase navigation,
object entity)
=> QueryBuffer.SetIsLoadedNoTracking(entity, (INavigation)navigation);

private static readonly MethodInfo _addToCollectionSnapshotMethodInfo
= typeof(IncludeLoadTreeNode).GetTypeInfo()
Expand Down
Loading