Template
Initial Commit
This commit is contained in:
commit
bd7e505432
22 files changed
+871
No files matched your search
@@ -0,0 +1,6 @@
|
||||
namespace RT.Infrastructure;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace RT.Infrastructure.Data;
|
||||
|
||||
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
|
||||
{
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using RT.Infrastructure.Data;
|
||||
|
||||
namespace RT.Infrastructure;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddDbContext<AppDbContext>(options =>
|
||||
options
|
||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||
.UseSnakeCaseNamingConvention());
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RT.Domain\RT.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EFCore.NamingConventions" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.11" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RT.Domain.Entities;
|
||||
using RT.Infrastructure.Repositories;
|
||||
using RT.Infrastructure.Data;
|
||||
|
||||
namespace RT.Infrastructure.Repositories;
|
||||
|
||||
public abstract class BaseRepository<T>(AppDbContext db) : IRepository<T> where T : BaseEntity
|
||||
{
|
||||
protected readonly DbSet<T> Set = db.Set<T>();
|
||||
|
||||
public virtual Task<T?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||
=> Set.FirstOrDefaultAsync(e => e.Id == id && !e.IsDeleted, ct);
|
||||
|
||||
public virtual async Task<IEnumerable<T>> GetAllAsync(CancellationToken ct = default)
|
||||
=> await Set.Where(e => !e.IsDeleted).ToListAsync(ct);
|
||||
|
||||
public virtual Task<int> CountAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken ct = default)
|
||||
=> predicate is null
|
||||
? Set.CountAsync(e => !e.IsDeleted, ct)
|
||||
: Set.Where(e => !e.IsDeleted).CountAsync(predicate, ct);
|
||||
|
||||
public virtual async Task InsertAsync(T entity, CancellationToken ct = default)
|
||||
{
|
||||
await Set.AddAsync(entity, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public virtual async Task UpdateAsync(T entity, CancellationToken ct = default)
|
||||
{
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
Set.Update(entity);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public virtual async Task RemoveAsync(T entity, CancellationToken ct = default)
|
||||
{
|
||||
entity.IsDeleted = true;
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public Task SaveChangesAsync(CancellationToken ct = default)
|
||||
=> db.SaveChangesAsync(ct);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Linq.Expressions;
|
||||
using RT.Domain.Entities;
|
||||
|
||||
namespace RT.Infrastructure.Repositories;
|
||||
|
||||
public interface IRepository<T> where T : BaseEntity
|
||||
{
|
||||
Task<T?> GetByIdAsync(Guid id, CancellationToken ct = default);
|
||||
Task<IEnumerable<T>> GetAllAsync(CancellationToken ct = default);
|
||||
Task<int> CountAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken ct = default);
|
||||
Task InsertAsync(T entity, CancellationToken ct = default);
|
||||
Task UpdateAsync(T entity, CancellationToken ct = default);
|
||||
Task RemoveAsync(T entity, CancellationToken ct = default);
|
||||
Task SaveChangesAsync(CancellationToken ct = default);
|
||||
}
|
||||
Reference in new issue
Block a user