Working in migration to linq2SQL

This commit is contained in:
2020-09-28 21:23:11 +02:00
parent 552e9cc6df
commit 9bf37f5799
17 changed files with 194 additions and 876 deletions

View File

@ -1,63 +0,0 @@
#nullable enable
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using UserService.DatabaseLayer.DataModels;
using UserService.Infrastructure.DataModels;
namespace UserService.DatabaseLayer.Repositories
{
public class BaseRepository<T> where T : Node
{
protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> 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 virtual 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)
{
await using var db = new UserServiceDbContext();
return await Context(db)
.Include(x => x.Parent)
.WhereOrDefault(predicate)
.ToListAsync(token)
.ConfigureAwait(false);
}
public virtual async Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await Context(db)
.Include(x => x.Parent)
.FirstOrDefaultAsync(predicate, token)
.ConfigureAwait(false);
}
public virtual async Task<bool> UpdateAsync(T entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
Context(db).Update(entity);
var items = await db.SaveChangesAsync(token).ConfigureAwait(false);
return items > 0;
}
}
}

View File

@ -4,11 +4,11 @@ using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using UserService.Infrastructure.DataModels;
using DataModels;
namespace UserService.DatabaseLayer.Repositories
{
public interface IRepository<T> where T : Node
public interface IRepository<T> where T : class
{
Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default);
Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default);

View File

@ -1,51 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using UserService.DatabaseLayer.DataModels;
using UserService.Infrastructure.DataModels;
using DataModels;
namespace UserService.DatabaseLayer.Repositories
{
public class OrganizationUnitsRepository : BaseRepository<OrganizationUnit>, IOrganizationUnitsRepository
public class OrganizationUnitsRepository : IOrganizationUnitsRepository
{
public OrganizationUnitsRepository() : base(x => x.OrganizationUnits)
/// <inheritdoc />
public async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(Expression<Func<OrganizationUnit, bool>>? predicate = null, CancellationToken token = default)
{
throw new NotImplementedException();
}
/// <inheritdoc cref="GetAllAsync" />
public override async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(
Expression<Func<OrganizationUnit, bool>>? predicate = null, CancellationToken token = default)
/// <inheritdoc />
public async Task<OrganizationUnit?> GetAsync(Expression<Func<OrganizationUnit, bool>> predicate, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
var rootOus = await Context(db)
.Include(x => x.Parent)
.WhereOrDefault(predicate)
.ToListAsync(token).ConfigureAwait(false);
throw new NotImplementedException();
}
IEnumerable<OrganizationUnit> Rec(Node node)
{
if (!(node is OrganizationUnit organizationUnit)) yield break;
yield return organizationUnit;
foreach (var ouChild in rootOus.Where(x => x.ParentId != null && x.ParentId == organizationUnit.Id))
{
foreach (var unit in Rec(ouChild))
{
yield return unit;
}
}
}
/// <inheritdoc />
public async Task AddAsync(OrganizationUnit entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
var result = new List<OrganizationUnit>();
foreach (var ou in rootOus.Where(x => x.ParentId is null))
{
result.AddRange(Rec(ou));
}
/// <inheritdoc />
public async Task<bool> UpdateAsync(OrganizationUnit entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
return result;
/// <inheritdoc />
public async Task DeleteAsync(OrganizationUnit entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,53 +1,44 @@
#nullable enable
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using UserService.DatabaseLayer.DataModels;
using UserService.Infrastructure.DataModels;
using DataModels;
namespace UserService.DatabaseLayer.Repositories
{
public class SecurityGroupsRepository : BaseRepository<SecurityGroup>, ISecurityGroupsRepository
public class SecurityGroupsRepository : ISecurityGroupsRepository
{
public SecurityGroupsRepository() : base(x => x.SecurityGroups)
/// <inheritdoc />
public async Task<IReadOnlyList<SecurityGroup>> GetAllAsync(Expression<Func<SecurityGroup, bool>>? predicate = null, CancellationToken token = default)
{
throw new NotImplementedException();
}
public override async Task<IReadOnlyList<SecurityGroup>> GetAllAsync(Expression<Func<SecurityGroup, bool>>? predicate = null, CancellationToken token = default)
/// <inheritdoc />
public 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)
.ThenInclude(x=> x.AttachedMember)
.WhereOrDefault(predicate)
.ToListAsync(token).ConfigureAwait(false);
throw new NotImplementedException();
}
public override async Task<SecurityGroup?> GetAsync(Expression<Func<SecurityGroup, bool>> predicate, CancellationToken token = default)
/// <inheritdoc />
public async Task AddAsync(SecurityGroup entity, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await Context(db)
.Include(x => x.Parent)
.Include(x => x.Members)
.ThenInclude(x=> x.AttachedMember)
.FirstOrDefaultAsync(predicate, token).ConfigureAwait(false);
throw new NotImplementedException();
}
public override async Task<bool> UpdateAsync(SecurityGroup entity, CancellationToken token = default)
/// <inheritdoc />
public async Task<bool> UpdateAsync(SecurityGroup entity, CancellationToken token = default)
{
if (entity == null) throw new ArgumentNullException(nameof(entity));
throw new NotImplementedException();
}
await using var db = new UserServiceDbContext();
Context(db).Update(entity);
db.UserMembers.UpdateRange(entity.Members);
var items = await db.SaveChangesAsync(token).ConfigureAwait(false);
return items > 0;
/// <inheritdoc />
public async Task DeleteAsync(SecurityGroup entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,11 +1,68 @@
using UserService.Infrastructure.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using DataModels;
using LinqToDB;
using LinqToDB.Data;
namespace UserService.DatabaseLayer.Repositories
{
public class UsersRepository : BaseRepository<User>, IUsersRepository
public class UsersRepository : IUsersRepository
{
public UsersRepository() : base(x => x.Users)
public UsersRepository()
{
DataConnection.DefaultSettings = new MySettings();
}
/// <inheritdoc />
public async Task<IReadOnlyList<User>> GetAllAsync(Expression<Func<User, bool>>? predicate = null,
CancellationToken token = default)
{
await using var db = new UserServiceDB();
return await db.Users.LoadWith(x=> x.Member).LoadWith(x=> x.Member.Node).WhereOrDefault(predicate).ToListAsync(token: token).ConfigureAwait(false);
}
/// <inheritdoc />
public async Task<User?> GetAsync(Expression<Func<User, bool>> predicate, CancellationToken token = default)
{
await using var db = new UserServiceDB();
var result = await db.Users.FirstOrDefaultAsync(predicate, token: token).ConfigureAwait(false);
return result;
}
/// <inheritdoc />
public async Task AddAsync(User entity, CancellationToken token = default)
{
await using var db = new UserServiceDB();
var nodeId =
await db.Nodes.InsertWithInt32IdentityAsync(() => new Node {CommonName = "holger"}, token: token);
await db.Members.InsertAsync(() => new Member {Id = nodeId}, token: token);
await db.Users.InsertAsync(() => new User
{
Id = nodeId,
FirstName = entity.FirstName,
LastName = entity.LastName,
IsActive = entity.IsActive
}, token: token).ConfigureAwait(false);
}
/// <inheritdoc />
public async Task<bool> UpdateAsync(User entity, CancellationToken token = default)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public async Task DeleteAsync(User entity, CancellationToken token = default)
{
await using var db = new UserServiceDB();
await db.Users.DeleteAsync(x => x.Id == entity.Id, token: token);
}
}
}