From 72603f836cd406180d264a9addebd939893feb3e Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 16 Apr 2025 09:12:06 +0200 Subject: [PATCH] Refactor repository pattern and dependency injection This commit introduces a new `DbRepository` class implementing the `IRepository` interface, providing asynchronous methods for CRUD operations. The previous CRUD repository registration in `DIExtensions.cs` has been removed, indicating a shift in repository management. A new static class `DependencyInjection` has been added to facilitate the registration of `DbRepository` with the service collection, enhancing modularity and flexibility in database interactions. --- .../DIExtensions.cs | 28 ------------ .../DbRepository.cs | 43 +++++++++++++++++++ .../DependencyInjection.cs | 17 ++++++++ 3 files changed, 60 insertions(+), 28 deletions(-) delete mode 100644 DigitalData.Core.Infrastructure/DIExtensions.cs create mode 100644 DigitalData.Core.Infrastructure/DbRepository.cs create mode 100644 DigitalData.Core.Infrastructure/DependencyInjection.cs diff --git a/DigitalData.Core.Infrastructure/DIExtensions.cs b/DigitalData.Core.Infrastructure/DIExtensions.cs deleted file mode 100644 index 3a2b7cd..0000000 --- a/DigitalData.Core.Infrastructure/DIExtensions.cs +++ /dev/null @@ -1,28 +0,0 @@ -using DigitalData.Core.Abstractions; -using DigitalData.Core.Abstractions.Infrastructure; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using System.DirectoryServices; - -namespace DigitalData.Core.Infrastructure -{ - public static class DIExtensions - { - /// - /// Adds a CRUD repository for a specific entity type to the service collection. - /// - /// The entity type for which the repository is registered. - /// The type of the entity's identifier. - /// The DbContext type used by the repository. - /// The to add the repository to. - /// An optional action to configure additional services for the repository. - /// The original instance, allowing further configuration. - public static IServiceCollection AddCleanCRUDRepository(this IServiceCollection services, Action? configureRepository = null) - where TCRUDRepository : CRUDRepository where TEntity : class, IUnique where TDbContext : DbContext - { - services.AddScoped, TCRUDRepository>(); - configureRepository?.Invoke(services); - return services; - } - } -} \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/DbRepository.cs b/DigitalData.Core.Infrastructure/DbRepository.cs new file mode 100644 index 0000000..c5be39b --- /dev/null +++ b/DigitalData.Core.Infrastructure/DbRepository.cs @@ -0,0 +1,43 @@ +using DigitalData.Core.Abstractions.Infrastructure; +using Microsoft.EntityFrameworkCore; +using System.Linq.Expressions; + +namespace DigitalData.Core.Infrastructure; + +public class DbRepository : IRepository where TDbContext : DbContext where TEntity : class +{ + protected TDbContext Context; + + protected DbSet Entities; + + public DbRepository(TDbContext context, Func> queryFactory) + { + Context = context; + Entities = queryFactory(context); + } + + public Task CreateAsync(TDto dto) + { + throw new NotImplementedException(); + } + + public Task CreateAsync(IEnumerable dtos) + { + throw new NotImplementedException(); + } + + public Task> DeleteAsync(Expression expression) + { + throw new NotImplementedException(); + } + + public Task> ReadAsync(Expression? expression = null) + { + throw new NotImplementedException(); + } + + public Task> UpdateAsync(TDto dto, Expression expression) + { + throw new NotImplementedException(); + } +} diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs new file mode 100644 index 0000000..0ffdd02 --- /dev/null +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -0,0 +1,17 @@ +using DigitalData.Core.Abstractions.Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace DigitalData.Core.Infrastructure; + +public static class DependencyInjection +{ + public static IServiceCollection AddDbRepository(this IServiceCollection services, Func> queryFactory) + where TDbContext : DbContext + where TEntity : DbContext + { + return services + .AddScoped, DbRepository>() + .AddSingleton(queryFactory); + } +} \ No newline at end of file