refactoring: reorganize code

This commit is contained in:
Holger Börchers 2020-09-11 22:46:29 +02:00
parent baec3160af
commit 6a1d069d98
2 changed files with 34 additions and 22 deletions

View File

@ -1,11 +1,12 @@
#nullable enable #nullable enable
using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using UserService.DatabaseLayer.DataModels; using UserService.DatabaseLayer.DataModels;
using UserService.Infrastructure.DataModels; using UserService.Infrastructure.DataModels;
@ -13,13 +14,26 @@ namespace UserService.DatabaseLayer.Repositories
{ {
public class BaseRepository<T> where T : Node public class BaseRepository<T> where T : Node
{ {
protected Func<UserServiceDbContext, DbSet<T>> Context { get; }
protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> context) protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> context)
{ {
Context = context; Context = context;
} }
protected Func<UserServiceDbContext, DbSet<T>> Context { get; }
public async Task AddAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
await Context(db).AddAsync(@entity, token).ConfigureAwait(false);
await db.SaveChangesAsync(token).ConfigureAwait(false);
}
public async Task DeleteAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
Context(db).Remove(entity);
await db.SaveChangesAsync(token).ConfigureAwait(false);
}
public virtual async Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default) public virtual async Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default)
{ {
await using var db = new UserServiceDbContext(); await using var db = new UserServiceDbContext();
@ -31,27 +45,12 @@ namespace UserService.DatabaseLayer.Repositories
await using var db = new UserServiceDbContext(); await using var db = new UserServiceDbContext();
return await Context(db).Include(x => x.Parent).FirstOrDefaultAsync(predicate, token).ConfigureAwait(false); return await Context(db).Include(x => x.Parent).FirstOrDefaultAsync(predicate, token).ConfigureAwait(false);
} }
public async Task AddAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
await Context(db).AddAsync(@entity, token).ConfigureAwait(false);
await db.SaveChangesAsync(token).ConfigureAwait(false);
}
public async Task<bool> UpdateAsync(T entity, CancellationToken token = default) public async Task<bool> UpdateAsync(T entity, CancellationToken token = default)
{ {
await using var db = new UserServiceDbContext(); await using var db = new UserServiceDbContext();
Context(db).Update(entity); Context(db).Update(entity);
var items= await db.SaveChangesAsync(token).ConfigureAwait(false); var items = await db.SaveChangesAsync(token).ConfigureAwait(false);
return items > 0; return items > 0;
} }
public async Task DeleteAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
Context(db).Remove(entity);
await db.SaveChangesAsync(token).ConfigureAwait(false);
}
} }
} }

View File

@ -1,9 +1,10 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using UserService.DatabaseLayer.DataModels; using UserService.DatabaseLayer.DataModels;
using UserService.Infrastructure.DataModels; using UserService.Infrastructure.DataModels;
@ -18,8 +19,20 @@ namespace UserService.DatabaseLayer.Repositories
public override async Task<IReadOnlyList<SecurityGroup>> GetAllAsync(Expression<Func<SecurityGroup, bool>>? predicate = null, CancellationToken token = default) public override async Task<IReadOnlyList<SecurityGroup>> GetAllAsync(Expression<Func<SecurityGroup, bool>>? predicate = null, CancellationToken token = default)
{ {
await using var db = new UserServiceDbContext(); await using var db = new UserServiceDbContext();
return await Context(db).Include(x => x.Parent).WhereOrDefault(predicate).ToListAsync(token).ConfigureAwait(false); return await Context(db)
.Include(x => x.Parent)
.Include(x => x.Members)
.WhereOrDefault(predicate)
.ToListAsync(token).ConfigureAwait(false);
} }
public override async Task<SecurityGroup?> GetAsync(Expression<Func<SecurityGroup, bool>> predicate, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await Context(db)
.Include(x => x.Parent)
.Include(x => x.Members)
.FirstOrDefaultAsync(predicate, token).ConfigureAwait(false);
}
} }
} }