Skip to content
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
7 changes: 6 additions & 1 deletion src/EFCore.Abstractions/Infrastructure/ILazyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure;
/// See <see href="https://aka.ms/efcore-docs-lazy-loading">Lazy loading</see> for more information and examples.
/// </para>
/// </remarks>
public interface ILazyLoader : IDisposable
public interface ILazyLoader
{
/// <summary>
/// Sets the given navigation as known to be completely loaded or known to be
Expand Down Expand Up @@ -66,4 +66,9 @@ Task LoadAsync(
object entity,
CancellationToken cancellationToken = default,
[CallerMemberName] string navigationName = "");

/// <summary>
/// Disposes the loader.
/// </summary>
void Dispose();
}
5 changes: 5 additions & 0 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ public virtual void Unsubscribe(bool resetting)
{
disposable.Dispose();
}
else if (resetting
&& service is ILazyLoader lazyLoader)
{
lazyLoader.Dispose();
}
else if (service is not IInjectableService detachable
|| detachable.Detaching(Context, entry.Entity))
{
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static readonly IDictionary<Type, ServiceCharacteristics> CoreServices
{ typeof(IDbContextLogger), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IAdHocMapper), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ILazyLoader), new ServiceCharacteristics(ServiceLifetime.Transient) },
{ typeof(ILazyLoaderFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IParameterBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(ITypeMappingSourcePlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{
Expand Down Expand Up @@ -285,12 +286,14 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IDesignTimeModel>(p => new DesignTimeModel(GetContextServices(p)));
TryAdd(p => GetContextServices(p).CurrentContext);
TryAdd<IDbContextOptions>(p => GetContextServices(p).ContextOptions);
TryAdd<IResettableService, ILazyLoaderFactory>(p => p.GetRequiredService<ILazyLoaderFactory>());
TryAdd<IResettableService, IStateManager>(p => p.GetRequiredService<IStateManager>());
TryAdd<IResettableService, IDbContextTransactionManager>(p => p.GetRequiredService<IDbContextTransactionManager>());
TryAdd<IEvaluatableExpressionFilter, EvaluatableExpressionFilter>();
TryAdd<IValueConverterSelector, ValueConverterSelector>();
TryAdd<IConstructorBindingFactory, ConstructorBindingFactory>();
TryAdd<ILazyLoader, LazyLoader>();
TryAdd<ILazyLoaderFactory, LazyLoaderFactory>();
TryAdd<ILazyLoader>(p => p.GetRequiredService<ILazyLoaderFactory>().Create());
TryAdd<IParameterBindingFactories, ParameterBindingFactories>();
TryAdd<IMemberClassifier, MemberClassifier>();
TryAdd<IPropertyParameterBindingFactory, PropertyParameterBindingFactory>();
Expand Down
21 changes: 21 additions & 0 deletions src/EFCore/Infrastructure/Internal/ILazyLoaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Infrastructure.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public interface ILazyLoaderFactory : IDisposable, IResettableService
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
ILazyLoader Create();
}
81 changes: 81 additions & 0 deletions src/EFCore/Infrastructure/Internal/LazyLoaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Infrastructure.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class LazyLoaderFactory : ILazyLoaderFactory
{
private readonly ICurrentDbContext _currentContext;
private readonly IDiagnosticsLogger<DbLoggerCategory.Infrastructure> _logger;
private readonly List<ILazyLoader> _loaders = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public LazyLoaderFactory(
ICurrentDbContext currentContext,
IDiagnosticsLogger<DbLoggerCategory.Infrastructure> logger)
{
_currentContext = currentContext;
_logger = logger;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ILazyLoader Create()
{
var loader = new LazyLoader(_currentContext, _logger);
_loaders.Add(loader);
return loader;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public void Dispose()
{
foreach (var loader in _loaders)
{
loader.Dispose();
}
_loaders.Clear();
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public void ResetState()
=> Dispose();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public Task ResetStateAsync(CancellationToken cancellationToken = default)
{
Dispose();

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static void TestMultipleScoped(Action<EntityFrameworkServicesBuilder> tr
{
services = context.GetService<IEnumerable<IResettableService>>().ToList();

Assert.Equal(3, services.Count);
Assert.Equal(4, services.Count);
Assert.Contains(typeof(FakeResetableService), services.Select(s => s.GetType()));
Assert.Contains(typeof(StateManager), services.Select(s => s.GetType()));
Assert.Contains(typeof(InMemoryTransactionManager), services.Select(s => s.GetType()));
Expand All @@ -281,7 +281,7 @@ private static void TestMultipleScoped(Action<EntityFrameworkServicesBuilder> tr
{
var newServices = context.GetService<IEnumerable<IResettableService>>().ToList();

Assert.Equal(3, newServices.Count);
Assert.Equal(4, newServices.Count);

foreach (var service in newServices)
{
Expand Down