Enhance DbRepository with AutoMapper integration

Added AutoMapper support by introducing an IMapper field.
Changed Context and Entities fields to readonly for better
encapsulation. Updated constructor to accept an IMapper
parameter, improving object mapping capabilities.
This commit is contained in:
Developer 02 2025-04-16 09:39:14 +02:00
parent 352b59dfdf
commit a7e4291e42

View File

@ -1,4 +1,5 @@
using DigitalData.Core.Abstractions.Infrastructure;
using AutoMapper;
using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
@ -6,14 +7,17 @@ namespace DigitalData.Core.Infrastructure;
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
{
protected TDbContext Context;
protected readonly TDbContext Context;
protected DbSet<TEntity> Entities;
protected readonly DbSet<TEntity> Entities;
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory)
protected readonly IMapper Mapper;
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IMapper mapper)
{
Context = context;
Entities = queryFactory(context);
Mapper = mapper;
}
public Task<TEntity> CreateAsync(TEntity dto, CancellationToken ct = default)