12 Commits

Author SHA1 Message Date
Developer 02
3ac0501231 chore: covert from debug to release to publish package 2025-04-22 18:11:34 +02:00
Developer 02
db8a560805 chore(Infrastructure.AutoMapper): Konfiguration für das Packen 2025-04-22 18:07:40 +02:00
Developer 02
e67361bfe1 chore(DigitalData.Core.Infrastructure): Hochgestuft auf 2.0.2 2025-04-22 18:00:29 +02:00
Developer 02
91594e80bf refactor(EntityConfigurationOptions): aktualisiert, um IServiceCollection mit Callback zu konfigurieren 2025-04-22 17:58:49 +02:00
Developer 02
8d98159ba8 fix: Korrektur der Update- und Löschlogik in DbRepository zur Vermeidung von Laufzeitproblemen 2025-04-22 17:33:55 +02:00
Developer 02
f1f5b9e16d refactor(DbRepositoryTests): Update AddDbRepository configuration 2025-04-22 16:32:17 +02:00
Developer 02
3955dede16 feat(EntityConfigurationOptions): Erstellt, um Entitäten wie Mapper konfigurieren zu können 2025-04-22 16:21:57 +02:00
Developer 02
65e834784a feat(EntityAutoMapper): Erstellt mit der Konfiguration der Dependency Injection. 2025-04-22 15:15:52 +02:00
Developer 02
3c1bbc1151 feat(Repository): CreateAsync-Methoden für DTO wurden in Erweiterungsmethoden konvertiert 2025-04-22 11:21:21 +02:00
Developer 02
5465fe5b49 feat(IEntityMapper): Erstellt, um Mapper zu abstrahieren.
- Integriert in IRepository und Repository
2025-04-22 11:10:55 +02:00
Developer 02
85787e7054 feat(DbRepositoryTests): ReadAsync_ShouldReturnUpdated und ReadAsync_ShouldNotReturnDeleted Tests 2025-04-22 10:09:47 +02:00
Developer 02
c955220310 feat (Mapping): Porfile hinzufügen 2025-04-22 09:53:20 +02:00
17 changed files with 282 additions and 47 deletions

View File

@@ -4,6 +4,21 @@ namespace DigitalData.Core.Abstractions.Infrastructure;
public static class Extensions
{
#region Create
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, TDto dto, CancellationToken ct = default)
{
var entity = repository.Mapper.Map(dto);
return repository.CreateAsync(entity, ct);
}
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, IEnumerable<TDto> dtos, CancellationToken ct = default)
{
var entities = dtos.Select(dto => repository.Mapper.Map(dto));
return repository.CreateAsync(entities, ct);
}
#endregion
#region Read
public static async Task<TEntity?> ReadFirstOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).FirstOrDefault();
@@ -15,4 +30,5 @@ public static class Extensions
public static async Task<TEntity> ReadSingleAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).Single();
#endregion
}

View File

@@ -0,0 +1,34 @@
namespace DigitalData.Core.Abstractions.Infrastructure
{
/// <summary>
/// Defines methods for mapping between entities and Data Transfer Objects (DTOs).
/// </summary>
/// <typeparam name="TEntity">The type of the entity to be mapped.</typeparam>
public interface IEntityMapper<TEntity>
{
/// <summary>
/// Maps an entity to a DTO.
/// </summary>
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
/// <param name="entity">The entity to be mapped.</param>
/// <returns>The mapped DTO.</returns>
TDto Map<TDto>(TEntity entity);
/// <summary>
/// Maps a DTO to an entity.
/// </summary>
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
/// <param name="dto">The DTO to be mapped.</param>
/// <returns>The mapped entity.</returns>
TEntity Map<TDto>(TDto dto);
/// <summary>
/// Maps a DTO to an existing entity.
/// </summary>
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
/// <param name="dto">The DTO to be mapped.</param>
/// <param name="entity">The existing entity to be updated with the mapped values.</param>
/// <returns>The updated entity.</returns>
TEntity Map<TDto>(TDto dto, TEntity entity);
}
}

View File

@@ -4,17 +4,15 @@ namespace DigitalData.Core.Abstractions.Infrastructure;
public interface IRepository<TEntity>
{
public IEntityMapper<TEntity> Mapper { get; }
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
public Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default);
public Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken ct = default);
public Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
public Task DeleteAsync<TDto>(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
}

View File

@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure.AutoMapper;
public static class DependencyInjection
{
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> options, params Type[] typeOfDtos)
{
options.AddCustomMapper<EntityAutoMapper<TEntity>>(services =>
{
if (typeOfDtos.Length != 0)
{
services.AddAutoMapper(cnf =>
{
foreach (var typeOfDto in typeOfDtos)
{
cnf.CreateMap(typeof(TEntity), typeOfDto);
cnf.CreateMap(typeOfDto, typeof(TEntity));
}
});
}
});
return options;
}
}

View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>DigitalData.Core.Infrastructure.AutoMapper</PackageId>
<Version>1.0.0</Version>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.Infrastructure.AutoMapper</Product>
<Description>This package provides AutoMapper support for the DigitalData.Core.Infrastructure module. It includes mapping configurations and abstractions used for object-to-object mapping within the infrastructure layer.</Description>
<Copyright>Copyright 2024</Copyright>
<PackageIcon>core_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<RepositoryType>digital data core abstractions clean architecture mapping</RepositoryType>
<PackageTags>digital data core infrastructure clean architecture mapping</PackageTags>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\nuget-package-icons\core_icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Infrastructure;
namespace DigitalData.Core.Infrastructure.AutoMapper;
public class EntityAutoMapper<TEntity> : IEntityMapper<TEntity>
{
private readonly IMapper _rootMapper;
public EntityAutoMapper(IMapper rootMapper)
{
_rootMapper = rootMapper;
}
public TDto Map<TDto>(TEntity entity) => _rootMapper.Map<TDto>(entity);
public TEntity Map<TDto>(TDto dto) => _rootMapper.Map<TEntity>(dto);
public TEntity Map<TDto>(TDto dto, TEntity entity) => _rootMapper.Map(dto, entity);
}

View File

@@ -1,5 +1,4 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Infrastructure;
using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
@@ -11,9 +10,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
protected internal readonly DbSet<TEntity> Entities;
protected internal readonly IMapper Mapper;
public IEntityMapper<TEntity> Mapper { get; }
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IMapper mapper)
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IEntityMapper<TEntity> mapper)
{
Context = context;
Entities = queryFactory(context);
@@ -34,18 +33,6 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
return entities;
}
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default)
{
var entity = Mapper.Map<TEntity>(dto);
return CreateAsync(entity, ct);
}
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken ct = default)
{
var entities = dtos.Select(dto => Mapper.Map<TEntity>(dto));
return CreateAsync(entities, ct);
}
public virtual async Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
=> expression is null
? await Entities.AsNoTracking().ToListAsync(ct)
@@ -55,22 +42,22 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
{
var entities = await Entities.Where(expression).ToListAsync(ct);
foreach (var entity in entities)
for (int i = entities.Count - 1; i >= 0; i--)
{
Mapper.Map(dto, entity);
Entities.Add(entity);
Mapper.Map(dto, entities[i]);
Entities.Update(entities[i]);
}
await Context.SaveChangesAsync(ct);
}
public virtual async Task DeleteAsync<TDto>(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
{
var entities = await Entities.Where(expression).ToListAsync(ct);
foreach (var entity in entities)
for (int i = entities.Count - 1; i >= 0; i--)
{
entities.Remove(entity);
Entities.Remove(entities[i]);
}
await Context.SaveChangesAsync(ct);

View File

@@ -6,12 +6,14 @@ namespace DigitalData.Core.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
public static EntityConfigurationOptions<TEntity> AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
where TDbContext : DbContext
where TEntity : class
{
return services
services
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
.AddSingleton(queryFactory);
return new EntityConfigurationOptions<TEntity>(services);
}
}

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>DigitalData.Core.Infrastructure</PackageId>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.Infrastructure</Product>
@@ -16,6 +16,8 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
<AssemblyVersion>2.0.2</AssemblyVersion>
<FileVersion>2.0.2</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@@ -0,0 +1,27 @@
using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure;
public class EntityConfigurationOptions<TEntity>
{
private readonly IServiceCollection _services;
public EntityConfigurationOptions(IServiceCollection services)
{
_services = services;
}
public EntityConfigurationOptions<TEntity> AddCustomMapper<TEntityMapper>(Action<IServiceCollection> configure, Func<IServiceProvider, TEntityMapper>? factory = null)
where TEntityMapper : class, IEntityMapper<TEntity>
{
configure(_services);
if (factory is null)
_services.AddSingleton<IEntityMapper<TEntity>, TEntityMapper>();
else
_services.AddSingleton(factory);
return this;
}
}

View File

@@ -26,6 +26,7 @@
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure.AutoMapper\DigitalData.Core.Infrastructure.AutoMapper.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
</ItemGroup>

View File

@@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using DigitalData.Core.Abstractions.Infrastructure;
using DigitalData.Core.Infrastructure.AutoMapper;
public class DbRepositoryTests
{
@@ -21,7 +22,7 @@ public class DbRepositoryTests
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
builder.Services.AddDbRepository<MockDbContext, User>(context => context.Users);
builder.Services.AddDbRepository<MockDbContext, User>(context => context.Users).UseAutoMapper(typeof(UserCreateDto), typeof(UserReadDto), typeof(UserBase));
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
@@ -51,19 +52,62 @@ public class DbRepositoryTests
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
}
[TestCase(true, TestName = "WhenGivenMultipleUsers")]
[TestCase(false, TestName = "WhenGivenSingleUser")]
public async Task ReadAsync_ShouldReturnCreated(bool multiple)
[TestCase(true, TestName = "WhenDtoUsed")]
[TestCase(false, TestName = "WhenEntityUsed")]
public async Task ReadAsync_ShouldReturnCreated(bool useDto)
{
// Arrange
var faker = Fake.CreateUserFaker();
var user = faker.Generate();
// Act
var createdUser = await _userRepo.CreateAsync(user);
var createdUser = useDto
? await _userRepo.CreateAsync(Fake.UserCreateDto)
: await _userRepo.CreateAsync(Fake.User);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
// Assert
Assert.That(createdUser, Is.EqualTo(readUser));
Assert.Multiple(() =>
{
Assert.That(readUser, Is.Not.Null);
Assert.That(readUser, Is.EqualTo(createdUser));
});
}
[Test]
public async Task ReadAsync_ShouldReturnUpdated()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
// Act
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id);
// Assert
Assert.Multiple(() =>
{
Assert.That(upToDateUser, Is.Not.Null);
Assert.That(upToDateUser?.FirstName, Is.EqualTo(userUpdateDto.FirstName));
Assert.That(upToDateUser?.Email, Is.EqualTo(userUpdateDto.Email));
Assert.That(upToDateUser?.Age, Is.EqualTo(userUpdateDto.Age));
});
}
[Test]
public async Task ReadAsync_ShouldNotReturnDeleted()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
// Act
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id);
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
// Assert
Assert.Multiple(() =>
{
Assert.That(readUser, Is.Not.Null);
Assert.That(deletedUser, Is.Null);
});
}
}

View File

@@ -9,8 +9,20 @@ public static class Fake
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
public static Faker<UserCreateDto> CreateUserDtoFaker(string? firstName = null, string? email = null, int? age = null) => new Faker<UserCreateDto>()
private static readonly Faker<User> UserFaker = CreateUserFaker();
public static User User => UserFaker.Generate();
public static List<User> Users => UserFaker.Generate(Random.Shared.Next(1, 10));
public static Faker<UserCreateDto> CreateUserCreateDtoFaker(string? firstName = null, string? email = null, int? age = null) => new Faker<UserCreateDto>()
.RuleFor(u => u.FirstName, f => firstName ?? f.Name.FirstName())
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
private static readonly Faker<UserCreateDto> UserCreateDtoFaker = CreateUserCreateDtoFaker();
public static UserCreateDto UserCreateDto => UserCreateDtoFaker.Generate();
public static List<UserCreateDto> UserCreateDtos => UserCreateDtoFaker.Generate(Random.Shared.Next(1, 10));
}

View File

@@ -0,0 +1,16 @@
using AutoMapper;
namespace DigitalData.Core.Tests.Mock;
public class MappingProfile : Profile
{
public MappingProfile()
{
// DTO ---> Entity
CreateMap<UserCreateDto, User>();
CreateMap<UserUpdateDto, User>();
// Entity ---> DTO
CreateMap<User, UserReadDto>();
}
}

View File

@@ -2,11 +2,11 @@
public class UserBase
{
public required string FirstName { get; init; }
public required string FirstName { get; set; }
public required string Email { get; init; }
public required string Email { get; set; }
public required int Age { get; init; }
public required int Age { get; set; }
public override int GetHashCode() => HashCode.Combine(FirstName, Email, Age);

View File

@@ -0,0 +1,5 @@
namespace DigitalData.Core.Tests.Mock;
public class UserUpdateDto : UserBase
{
}

View File

@@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Infrastructure.AutoMapper", "DigitalData.Core.Infrastructure.AutoMapper\DigitalData.Core.Infrastructure.AutoMapper.csproj", "{CE00E1F7-2771-4D9C-88FB-E564894E539E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{41795B74-A757-4E93-B907-83BFF04EEE5C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -92,12 +96,16 @@ Global
{C9266749-9504-4EA9-938F-F083357B60B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.Build.0 = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Debug|Any CPU.Build.0 = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A765EBEA-3D1E-4F36-869B-6D72F87FF3F6} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{A765EBEA-3D1E-4F36-869B-6D72F87FF3F6} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
{DB404CD9-CBB8-4771-AB1B-FD4FDE2C28CC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{C57B2480-F632-4691-9C4C-8CC01237203C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{B54DEF90-C30C-44EA-9875-76F1B330CBB7} = {EDF84A84-1E01-484E-B073-383F7139C891}
@@ -110,7 +118,9 @@ Global
{0FA93730-8084-4907-B172-87D610323796} = {EDF84A84-1E01-484E-B073-383F7139C891}
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6} = {EDF84A84-1E01-484E-B073-383F7139C891}
{72CBAFBA-55CC-49C9-A484-F8F4550054CB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{C9266749-9504-4EA9-938F-F083357B60B7} = {72CBAFBA-55CC-49C9-A484-F8F4550054CB}
{C9266749-9504-4EA9-938F-F083357B60B7} = {00000000-0000-0000-0000-000000000000}
{CE00E1F7-2771-4D9C-88FB-E564894E539E} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
{41795B74-A757-4E93-B907-83BFF04EEE5C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}