Compare commits
16 Commits
7d2098092a
...
feat/Infra
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb9449d701 | ||
|
|
72f735272f | ||
|
|
304f5b7b4c | ||
|
|
0238310290 | ||
|
|
3ac0501231 | ||
|
|
db8a560805 | ||
|
|
e67361bfe1 | ||
|
|
91594e80bf | ||
|
|
8d98159ba8 | ||
|
|
f1f5b9e16d | ||
|
|
3955dede16 | ||
|
|
65e834784a | ||
|
|
3c1bbc1151 | ||
|
|
5465fe5b49 | ||
|
|
85787e7054 | ||
|
|
c955220310 |
@@ -5,9 +5,9 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<PackageId>DigitalData.Core.Abstractions.Security</PackageId>
|
<PackageId>DigitalData.Core.Abstractions.Security</PackageId>
|
||||||
<Version>1.0.0</Version>
|
<Version>1.0.1</Version>
|
||||||
<AssemblyVersion>1.0.0</AssemblyVersion>
|
<AssemblyVersion>1.0.1</AssemblyVersion>
|
||||||
<FileVersion>1.0.0</FileVersion>
|
<FileVersion>1.0.1</FileVersion>
|
||||||
<Authors>Digital Data GmbH</Authors>
|
<Authors>Digital Data GmbH</Authors>
|
||||||
<Company>Digital Data GmbH</Company>
|
<Company>Digital Data GmbH</Company>
|
||||||
<Product>Digital Data GmbH</Product>
|
<Product>Digital Data GmbH</Product>
|
||||||
|
|||||||
@@ -17,9 +17,9 @@
|
|||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<PackAsTool>False</PackAsTool>
|
<PackAsTool>False</PackAsTool>
|
||||||
<PackageIcon>core_icon.png</PackageIcon>
|
<PackageIcon>core_icon.png</PackageIcon>
|
||||||
<Version>3.4.1</Version>
|
<Version>3.4.3</Version>
|
||||||
<AssemblyVersion>3.4.1</AssemblyVersion>
|
<AssemblyVersion>3.4.3</AssemblyVersion>
|
||||||
<FileVersion>3.4.1</FileVersion>
|
<FileVersion>3.4.3</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,15 +4,31 @@ namespace DigitalData.Core.Abstractions.Infrastructure;
|
|||||||
|
|
||||||
public static class Extensions
|
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)
|
public static async Task<TEntity?> ReadFirstOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||||
=> (await repository.ReadAsync(expression)).FirstOrDefault();
|
=> (await repository.ReadAllAsync(expression)).FirstOrDefault();
|
||||||
|
|
||||||
public static async Task<TEntity> ReadFirstAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
public static async Task<TEntity> ReadFirstAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||||
=> (await repository.ReadAsync(expression)).First();
|
=> (await repository.ReadAllAsync(expression)).First();
|
||||||
|
|
||||||
public static async Task<TEntity?> ReadSingleOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
public static async Task<TEntity?> ReadSingleOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||||
=> (await repository.ReadAsync(expression)).SingleOrDefault();
|
=> (await repository.ReadAllAsync(expression)).SingleOrDefault();
|
||||||
|
|
||||||
public static async Task<TEntity> ReadSingleAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
public static async Task<TEntity> ReadSingleAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||||
=> (await repository.ReadAsync(expression)).Single();
|
=> (await repository.ReadAllAsync(expression)).Single();
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
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 an entity list to a DTO list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
|
||||||
|
/// <param name="entities">The entity list to be mapped.</param>
|
||||||
|
/// <returns>The mapped DTO list.</returns>
|
||||||
|
IEnumerable<TDto> Map<TDto>(IEnumerable<TEntity> entities);
|
||||||
|
|
||||||
|
/// <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 list to an entity list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||||
|
/// <param name="dtos">The DTO list to be mapped.</param>
|
||||||
|
/// <returns>The mapped entity list.</returns>
|
||||||
|
IEnumerable<TEntity> Map<TDto>(IEnumerable<TDto> dtos);
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,17 +4,21 @@ namespace DigitalData.Core.Abstractions.Infrastructure;
|
|||||||
|
|
||||||
public interface IRepository<TEntity>
|
public interface IRepository<TEntity>
|
||||||
{
|
{
|
||||||
|
public IEntityMapper<TEntity> Mapper { get; }
|
||||||
|
|
||||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
|
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
|
||||||
|
|
||||||
|
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default);
|
public Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken ct = default);
|
public Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.2</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.2</AssemblyVersion>
|
||||||
|
<FileVersion>1.0.2</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>
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
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 IEnumerable<TDto> Map<TDto>(IEnumerable<TEntity> entities) => _rootMapper.Map<IEnumerable<TDto>>(entities);
|
||||||
|
|
||||||
|
public TEntity Map<TDto>(TDto dto) => _rootMapper.Map<TEntity>(dto);
|
||||||
|
|
||||||
|
public IEnumerable<TEntity> Map<TDto>(IEnumerable<TDto> dtos) => _rootMapper.Map<IEnumerable<TEntity>>(dtos);
|
||||||
|
|
||||||
|
public TEntity Map<TDto>(TDto dto, TEntity entity) => _rootMapper.Map(dto, entity);
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using AutoMapper;
|
using DigitalData.Core.Abstractions.Infrastructure;
|
||||||
using DigitalData.Core.Abstractions.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Linq.Expressions;
|
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 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;
|
Context = context;
|
||||||
Entities = queryFactory(context);
|
Entities = queryFactory(context);
|
||||||
@@ -34,43 +33,45 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default)
|
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, 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
|
=> expression is null
|
||||||
? await Entities.AsNoTracking().ToListAsync(ct)
|
? await Entities.AsNoTracking().ToListAsync(ct)
|
||||||
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
|
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
|
||||||
|
|
||||||
|
public virtual async Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||||
|
=> single
|
||||||
|
? await Entities.AsNoTracking().Where(expression).SingleOrDefaultAsync(ct)
|
||||||
|
: await Entities.AsNoTracking().Where(expression).FirstOrDefaultAsync(ct);
|
||||||
|
|
||||||
|
public virtual async Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
|
||||||
|
=> Mapper.Map<TDto>(await ReadAllAsync(expression, ct));
|
||||||
|
|
||||||
|
public virtual async Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var entity = await ReadOrDefaultAsync(expression, single, ct);
|
||||||
|
return entity is null ? default : Mapper.Map<TDto>(entity);
|
||||||
|
}
|
||||||
|
|
||||||
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
|
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entities = await Entities.Where(expression).ToListAsync(ct);
|
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);
|
Mapper.Map(dto, entities[i]);
|
||||||
Entities.Add(entity);
|
Entities.Update(entities[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Context.SaveChangesAsync(ct);
|
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);
|
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);
|
await Context.SaveChangesAsync(ct);
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ namespace DigitalData.Core.Infrastructure;
|
|||||||
|
|
||||||
public static class DependencyInjection
|
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 TDbContext : DbContext
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
{
|
{
|
||||||
return services
|
services
|
||||||
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
|
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
|
||||||
.AddSingleton(queryFactory);
|
.AddSingleton(queryFactory);
|
||||||
|
|
||||||
|
return new EntityConfigurationOptions<TEntity>(services);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||||
<PackageId>DigitalData.Core.Infrastructure</PackageId>
|
<PackageId>DigitalData.Core.Infrastructure</PackageId>
|
||||||
<Version>2.0.1</Version>
|
<Version>2.0.4</Version>
|
||||||
<Authors>Digital Data GmbH</Authors>
|
<Authors>Digital Data GmbH</Authors>
|
||||||
<Company>Digital Data GmbH</Company>
|
<Company>Digital Data GmbH</Company>
|
||||||
<Product>DigitalData.Core.Infrastructure</Product>
|
<Product>DigitalData.Core.Infrastructure</Product>
|
||||||
@@ -16,6 +16,8 @@
|
|||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
|
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
|
||||||
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
|
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
|
||||||
|
<AssemblyVersion>2.0.4</AssemblyVersion>
|
||||||
|
<FileVersion>2.0.4</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.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.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using DigitalData.Core.Abstractions.Infrastructure;
|
using DigitalData.Core.Abstractions.Infrastructure;
|
||||||
|
using DigitalData.Core.Infrastructure.AutoMapper;
|
||||||
|
|
||||||
public class DbRepositoryTests
|
public class DbRepositoryTests
|
||||||
{
|
{
|
||||||
@@ -21,7 +22,7 @@ public class DbRepositoryTests
|
|||||||
|
|
||||||
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
|
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());
|
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||||
|
|
||||||
@@ -51,19 +52,62 @@ public class DbRepositoryTests
|
|||||||
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
|
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(true, TestName = "WhenGivenMultipleUsers")]
|
[TestCase(true, TestName = "WhenDtoUsed")]
|
||||||
[TestCase(false, TestName = "WhenGivenSingleUser")]
|
[TestCase(false, TestName = "WhenEntityUsed")]
|
||||||
public async Task ReadAsync_ShouldReturnCreated(bool multiple)
|
public async Task ReadAsync_ShouldReturnCreated(bool useDto)
|
||||||
{
|
{
|
||||||
// Arrange
|
|
||||||
var faker = Fake.CreateUserFaker();
|
|
||||||
var user = faker.Generate();
|
|
||||||
|
|
||||||
// Act
|
// 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);
|
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
|
||||||
|
|
||||||
// Assert
|
// 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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,20 @@ public static class Fake
|
|||||||
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
|
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
|
||||||
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
|
.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.FirstName, f => firstName ?? f.Name.FirstName())
|
||||||
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
|
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
|
||||||
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
|
.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));
|
||||||
}
|
}
|
||||||
|
|||||||
16
DigitalData.Core.Tests/Mock/MappingProfile.cs
Normal file
16
DigitalData.Core.Tests/Mock/MappingProfile.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
public class UserBase
|
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);
|
public override int GetHashCode() => HashCode.Combine(FirstName, Email, Age);
|
||||||
|
|
||||||
|
|||||||
5
DigitalData.Core.Tests/Mock/UserUpdateDto.cs
Normal file
5
DigitalData.Core.Tests/Mock/UserUpdateDto.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
namespace DigitalData.Core.Tests.Mock;
|
||||||
|
|
||||||
|
public class UserUpdateDto : UserBase
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}"
|
||||||
EndProject
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
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}
|
{DB404CD9-CBB8-4771-AB1B-FD4FDE2C28CC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||||
{C57B2480-F632-4691-9C4C-8CC01237203C} = {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}
|
{B54DEF90-C30C-44EA-9875-76F1B330CBB7} = {EDF84A84-1E01-484E-B073-383F7139C891}
|
||||||
@@ -111,6 +119,8 @@ Global
|
|||||||
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6} = {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}
|
{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} = {72CBAFBA-55CC-49C9-A484-F8F4550054CB}
|
||||||
|
{CE00E1F7-2771-4D9C-88FB-E564894E539E} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
|
||||||
|
{41795B74-A757-4E93-B907-83BFF04EEE5C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}
|
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}
|
||||||
|
|||||||
Reference in New Issue
Block a user