From 561a751de40c6b321ba34244485aa9b4c99cfd9d Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 17 Apr 2025 15:05:23 +0200 Subject: [PATCH] Update dependency injection and repository tests Modified `AddDbRepository` method to allow broader entity types and registered `queryFactory` as a singleton. Added necessary using directives in `DbRepositoryTests.cs` and updated the `Setup` method to configure an in-memory database for testing with `MockDbContext` and `User` entity. --- DigitalData.Core.Infrastructure/DependencyInjection.cs | 2 +- .../Infrastructure/DbRepositoryTests.cs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs index 0ffdd02..9d656bd 100644 --- a/DigitalData.Core.Infrastructure/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -8,7 +8,7 @@ public static class DependencyInjection { public static IServiceCollection AddDbRepository(this IServiceCollection services, Func> queryFactory) where TDbContext : DbContext - where TEntity : DbContext + where TEntity : class { return services .AddScoped, DbRepository>() diff --git a/DigitalData.Core.Tests/Infrastructure/DbRepositoryTests.cs b/DigitalData.Core.Tests/Infrastructure/DbRepositoryTests.cs index b3b2ef5..0e2f643 100644 --- a/DigitalData.Core.Tests/Infrastructure/DbRepositoryTests.cs +++ b/DigitalData.Core.Tests/Infrastructure/DbRepositoryTests.cs @@ -1,5 +1,9 @@ namespace DigitalData.Core.Tests.Infrastructure; +using DigitalData.Core.Infrastructure; +using DigitalData.Core.Tests.Mock; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public class DbRepositoryTests @@ -11,6 +15,10 @@ public class DbRepositoryTests { var builder = Host.CreateApplicationBuilder(); + builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("MockDB")); + + builder.Services.AddDbRepository(context => context.Users); + _host = builder.Build(); }