reorganization of database layer

This commit is contained in:
2020-07-25 22:15:58 +02:00
parent 2a86c16b85
commit 110663456d
25 changed files with 471 additions and 370 deletions

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using UserService.DatabaseLayer.DataModels;
namespace UserService.DatabaseLayer.Repository
{
public class BaseRepository<T> where T : class
{
private readonly Func<UserServiceDbContext, DbSet<T>> _context;
protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> context)
{
_context = context;
}
public async Task<IReadOnlyList<T>> GetAllAsync(CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await _context(db).ToListAsync(token);
}
public async Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await _context(db).FirstOrDefaultAsync(predicate, token);
}
public async Task AddAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
await _context(db).AddAsync(@entity, token);
await db.SaveChangesAsync(token);
}
public async Task UpdateAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
_context(db).Update(entity);
await db.SaveChangesAsync(token);
}
public async Task DeleteAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
_context(db).Remove(entity);
await db.SaveChangesAsync(token);
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using UserService.DatabaseLayer.DataModels;
namespace UserService.DatabaseLayer.Repository
{
public interface IRepository<T> where T : Node
{
Task<IReadOnlyList<T>> GetAllAsync(CancellationToken token = default);
Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default);
Task AddAsync(T entity, CancellationToken token = default);
Task UpdateAsync(T entity, CancellationToken token = default);
Task DeleteAsync(T entity, CancellationToken token = default);
}
public interface IOrganizationUnitsRepository : IRepository<OrganizationUnit>
{
}
public interface ISecurityGroupsRepository : IRepository<SecurityGroup>
{
}
public interface IUsersRepository : IRepository<User>
{
}
public interface INodesRepository : IRepository<Node>
{
}
}

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using UserService.DatabaseLayer.DataModels;
namespace UserService.DatabaseLayer.Repository
{
public class OrganizationUnitsRepository : BaseRepository<OrganizationUnit>, IOrganizationUnitsRepository
{
public OrganizationUnitsRepository() : base(x => x.OrganizationUnits)
{
}
}
public class SecurityGroupsRepository : BaseRepository<SecurityGroup>, ISecurityGroupsRepository
{
public SecurityGroupsRepository() : base(x=> x.SecurityGroups)
{
}
}
public class UsersRepository : BaseRepository<User>, IUsersRepository
{
public UsersRepository() : base(x => x.Users)
{
}
}
public class NodesRepository : INodesRepository
{
private readonly IUsersRepository _users;
private readonly ISecurityGroupsRepository _securityGroups;
private readonly IOrganizationUnitsRepository _organizationUnits;
public NodesRepository(IUsersRepository users, ISecurityGroupsRepository securityGroups, IOrganizationUnitsRepository organizationUnits)
{
_users = users;
_securityGroups = securityGroups;
_organizationUnits = organizationUnits;
}
public static async IAsyncEnumerable<Node> GetNodesAsync([EnumeratorCancellation] CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
await foreach (var note in db.OrganizationUnits.AsAsyncEnumerable().WithCancellation(token))
{
yield return note;
}
await foreach (var node in db.SecurityGroups.AsAsyncEnumerable().WithCancellation(token))
{
yield return node;
}
await foreach (var node in db.Users.AsAsyncEnumerable().WithCancellation(token))
{
yield return node;
}
}
public async Task<IReadOnlyList<Node>> GetAllAsync(CancellationToken token = default)
{
var list = new List<Node>();
await foreach (var node in GetNodesAsync(token))
{
list.Add(node);
}
return list;
}
public Task<Node?> GetAsync(Expression<Func<Node, bool>> predicate, CancellationToken token = default)
{
throw new NotImplementedException();
}
public Task AddAsync(Node entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
public Task UpdateAsync(Node entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
public Task DeleteAsync(Node entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
}
}