Refactor repository pattern and dependency injection
This commit introduces a new `DbRepository` class implementing the `IRepository<TEntity>` 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.
This commit is contained in:
parent
162da9a16c
commit
72603f836c
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a CRUD repository for a specific entity type to the service collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The entity type for which the repository is registered.</typeparam>
|
||||
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
||||
/// <typeparam name="TDbContext">The DbContext type used by the repository.</typeparam>
|
||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the repository to.</param>
|
||||
/// <param name="configureRepository">An optional action to configure additional services for the repository.</param>
|
||||
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
|
||||
public static IServiceCollection AddCleanCRUDRepository<TEntity, TId, TDbContext, TCRUDRepository>(this IServiceCollection services, Action<IServiceCollection>? configureRepository = null)
|
||||
where TCRUDRepository : CRUDRepository<TEntity, TId, TDbContext> where TEntity : class, IUnique<TId> where TDbContext : DbContext
|
||||
{
|
||||
services.AddScoped<ICRUDRepository<TEntity, TId>, TCRUDRepository>();
|
||||
configureRepository?.Invoke(services);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
DigitalData.Core.Infrastructure/DbRepository.cs
Normal file
43
DigitalData.Core.Infrastructure/DbRepository.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using DigitalData.Core.Abstractions.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
|
||||
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
|
||||
{
|
||||
protected TDbContext Context;
|
||||
|
||||
protected DbSet<TEntity> Entities;
|
||||
|
||||
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory)
|
||||
{
|
||||
Context = context;
|
||||
Entities = queryFactory(context);
|
||||
}
|
||||
|
||||
public Task<TEntity> CreateAsync<TDto>(TDto dto)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<TEntity> CreateAsync<TDto>(IEnumerable<TDto> dtos)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TEntity>> DeleteAsync<TDto>(Expression expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TEntity>> ReadAsync(Expression? expression = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TEntity>> UpdateAsync<TDto>(TDto dto, Expression expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
17
DigitalData.Core.Infrastructure/DependencyInjection.cs
Normal file
17
DigitalData.Core.Infrastructure/DependencyInjection.cs
Normal file
@ -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<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
|
||||
where TDbContext : DbContext
|
||||
where TEntity : DbContext
|
||||
{
|
||||
return services
|
||||
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
|
||||
.AddSingleton(queryFactory);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user