Skip to content

Commit

Permalink
update DbContextInterceptorSamples.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jan 12, 2025
1 parent 015fa8d commit cb93df5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using WeihanLi.Extensions;

namespace WeihanLi.EntityFramework.Sample;

public static class DbContextInterceptorSamples
{
public static async Task RunAsync()
{
await InterceptorTest2();
}

private static async Task InterceptorTest1()
{
var services = new ServiceCollection();
services.AddScoped<SavingInterceptor>();
services.AddDbContext<FileTestDbContext>((provider, options) =>
{
options.AddInterceptors(provider.GetRequiredService<SavingInterceptor>());
options.UseInMemoryDatabase("test");
});
await using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<FileTestDbContext>();
await dbContext.Database.EnsureCreatedAsync();
dbContext.Entities.Add(new TestEntity { Id = 1, Name = "1" });
await dbContext.SaveChangesAsync();
}

private static async Task InterceptorTest2()
{
var services = new ServiceCollection();
services.AddDbContext<FileTestDbContext>(options =>
{
options.UseInMemoryDatabase("test");
});
services.AddDbContextInterceptor<FileTestDbContext, SavingInterceptor>();
await using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<FileTestDbContext>();
await dbContext.Database.EnsureCreatedAsync();
dbContext.Entities.Add(new TestEntity { Id = 2, Name = "1" });
await dbContext.SaveChangesAsync();
}
}

file sealed class FileTestDbContext(DbContextOptions<FileTestDbContext> options) : DbContext(options)
{
public DbSet<TestEntity> Entities { get; set; }
}

file sealed class TestEntity
{
public int Id { get; set; }
public string Name { get; set; }

Check warning on line 57 in samples/WeihanLi.EntityFramework.Sample/DbContextInterceptorSamples.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in samples/WeihanLi.EntityFramework.Sample/DbContextInterceptorSamples.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in samples/WeihanLi.EntityFramework.Sample/DbContextInterceptorSamples.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in samples/WeihanLi.EntityFramework.Sample/DbContextInterceptorSamples.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}

file sealed class SavingInterceptor : SaveChangesInterceptor
{
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = default)
{
Console.WriteLine("SavingChangesAsync");
return base.SavingChangesAsync(eventData, result, cancellationToken);
}

public override ValueTask<int> SavedChangesAsync(SaveChangesCompletedEventData eventData, int result,
CancellationToken cancellationToken = default)
{
Console.WriteLine("SavedChangesAsync");
return base.SavedChangesAsync(eventData, result, cancellationToken);
}
}
10 changes: 6 additions & 4 deletions samples/WeihanLi.EntityFramework.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ namespace WeihanLi.EntityFramework.Sample;

public static class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
// SoftDeleteTest();
// RepositoryTest();
AutoAuditTest();
// AutoAuditTest();

await DbContextInterceptorSamples.RunAsync();

Console.WriteLine("completed");
Console.ReadLine();
Expand Down Expand Up @@ -116,7 +118,7 @@ private static void AutoAuditTest()
// .AddInterceptors(ActivatorUtilities.GetServiceOrCreateInstance<AuditInterceptor>(provider))
;
});
services.AddDbContextInterceptor<TestDbContext, AuditInterceptor>();
services.AddDbContextInterceptor1<TestDbContext, AuditInterceptor>();

services.AddEFAutoAudit(builder =>
{
Expand Down Expand Up @@ -435,7 +437,7 @@ private static void SoftDeleteTest()
context.Database.EnsureDeleted();
}

private static IServiceCollection AddDbContextInterceptor<TContext, TInterceptor>(
private static IServiceCollection AddDbContextInterceptor1<TContext, TInterceptor>(
this IServiceCollection services,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped
)
Expand Down

0 comments on commit cb93df5

Please sign in to comment.