Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Sep 29, 2021
1 parent 74f8bf5 commit e9a1508
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
3 changes: 2 additions & 1 deletion samples/HelloCart/v2/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Stl.Fusion.EntityFramework;
using Stl.Fusion.EntityFramework.Operations;

namespace Samples.HelloCart.V2
Expand All @@ -24,7 +25,7 @@ public class DbCartItem
public decimal Quantity { get; set; }
}

public class AppDbContext : DbContext
public class AppDbContext : DbContextBase
{
public DbSet<DbProduct> Products { get; protected set; } = null!;
public DbSet<DbCart> Carts { get; protected set; } = null!;
Expand Down
3 changes: 2 additions & 1 deletion samples/TodoApp/Services/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Stl.Fusion.EntityFramework;
using Stl.Fusion.EntityFramework.Authentication;
using Stl.Fusion.EntityFramework.Extensions;
using Stl.Fusion.EntityFramework.Operations;

namespace Templates.TodoApp.Services
{
public class AppDbContext : DbContext
public class AppDbContext : DbContextBase
{
// Stl.Fusion.EntityFramework tables
public DbSet<DbUser<string>> Users { get; protected set; } = null!;
Expand Down
57 changes: 57 additions & 0 deletions src/Stl.Fusion.EntityFramework/DbContextBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Stl.Fusion.EntityFramework
{
/// <summary>
/// This type solves a single problem: currently EF Core 6.0
/// doesn't properly dispose pooled DbContexts rendering
/// them unusable after disposal.
/// Details: https://github.com/dotnet/efcore/issues/26202
/// </summary>
public abstract class DbContextBase :
#if NET6_0
DbContext, IResettableService
#else
DbContext
#endif
{
#if NET6_0
private static readonly FieldInfo DisposedField =
typeof(DbContext).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic)!;
private static readonly MethodInfo GetResettableServicesMethod =
typeof(DbContext).GetMethod("GetResettableServices", BindingFlags.Instance | BindingFlags.NonPublic)!;
#endif

protected DbContextBase() { }
protected DbContextBase(DbContextOptions options) : base(options) { }

#if NET6_0
void IResettableService.ResetState()
{
var services = GetResettableServices2();
foreach (var service in services)
service.ResetState();
DisposedField.SetValue(this, false);
}

async Task IResettableService.ResetStateAsync(CancellationToken cancellationToken = default)
{
var services = GetResettableServices2();
foreach (var service in services)
await service.ResetStateAsync(cancellationToken).ConfigureAwait(false);
DisposedField.SetValue(this, false);
}

private IEnumerable<IResettableService> GetResettableServices2()
=> (IEnumerable<IResettableService>) GetResettableServicesMethod.Invoke(
this,
Array.Empty<object>())!;
#endif
}
}
2 changes: 1 addition & 1 deletion tests/Stl.Fusion.Tests/Model/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DbAuthUser : DbUser<long>
public class DbAuthSessionInfo : DbSessionInfo<long>
{ }

public class TestDbContext : DbContext
public class TestDbContext : DbContextBase
{
public DbSet<User> Users { get; protected set; } = null!;
public DbSet<Message> Messages { get; protected set; } = null!;
Expand Down
4 changes: 2 additions & 2 deletions tests/Stl.Fusion.Tests/PerformanceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public override async Task InitializeAsync()
await Task.WhenAll(tasks);
}

// [Fact]
[Fact(Skip = "Performance")]
[Fact]
// [Fact(Skip = "Performance")]
public async Task ComputedPerformanceTest()
{
if (TestRunnerInfo.IsBuildAgent())
Expand Down
4 changes: 2 additions & 2 deletions tests/Stl.Fusion.Tests/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public virtual async Task<bool> Delete(IUserService.DeleteCommand command, Cance
{
// Debug.WriteLine($"TryGetAsync {userId}");
await Everything().ConfigureAwait(false);
await using var dbContext = DbContextFactory.CreateDbContext();
await using var dbContext = CreateDbContext();
var user = await dbContext.Users
.FindAsync(new[] {(object) userId}, cancellationToken)
.ConfigureAwait(false);
Expand All @@ -145,7 +145,7 @@ public virtual async Task<bool> Delete(IUserService.DeleteCommand command, Cance
public virtual async Task<long> Count(CancellationToken cancellationToken = default)
{
await Everything().ConfigureAwait(false);
await using var dbContext = DbContextFactory.CreateDbContext();
await using var dbContext = CreateDbContext();
var count = await dbContext.Users.LongCountAsync(cancellationToken).ConfigureAwait(false);
// _log.LogDebug($"Users.Count query: {count}");
return count;
Expand Down
3 changes: 2 additions & 1 deletion tests/Stl.Tests/CommandR/Services/Db/TestDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Stl.Fusion.EntityFramework;
using Stl.Fusion.EntityFramework.Operations;

namespace Stl.Tests.CommandR.Services
{
public class TestDbContext : DbContext
public class TestDbContext : DbContextBase
{
public DbSet<DbOperation> Operations { get; protected set; } = null!;
public DbSet<User> Users { get; protected set; } = null!;
Expand Down

0 comments on commit e9a1508

Please sign in to comment.