Skip to content

Commit

Permalink
2.0 alpha 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bdongus committed Dec 22, 2023
1 parent e9c3282 commit 2b94a6f
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 98 deletions.
25 changes: 14 additions & 11 deletions idee5.Common.Data.Tests/ACompositeRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal class MeUserProvider : ICurrentUserIdProvider {
public string GetCurrentUserId() => "me";
}
#pragma warning disable CS0618 // Typ oder Element ist veraltet

internal class TestRepository : ACompositeKeyRepository<TestEntity> {
private readonly AmbientTimeProvider _timeProvider;
private readonly AmbientUserProvider _currentUserIdProvider;
Expand All @@ -33,10 +32,6 @@ public TestRepository(AmbientTimeProvider timeProvider, AmbientUserProvider curr

public override void Add(TestEntity item) => TestEntities.Add(item);

public override Task<IEnumerable<TestEntity>> GetAsync(Func<IQueryable<TestEntity>, IQueryable<TestEntity>> func, CancellationToken cancellationToken = default) {
return Task.Run(() => func(TestEntities.AsQueryable()).AsEnumerable(), cancellationToken);
}

public override void Remove(TestEntity item) => TestEntities.Remove(item);

public override Task RemoveAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default)
Expand All @@ -63,16 +58,24 @@ public override Task UpdateOrAddAsync(TestEntity item, CancellationToken cancell
return Task.CompletedTask;
}

public override Task<bool> ExistsAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
public override Task<List<TestEntity>> GetAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities.Where(predicate.Compile()).ToList());
}

public override Task<bool> ExistsAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<int> CountAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
public override Task<int> CountAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<TestEntity> GetSingleAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities.SingleOrDefault(predicate));
public override Task<TestEntity> GetSingleAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities.SingleOrDefault(predicate.Compile()));
}

public override Task<List<TestEntity>> GetAllAsync(CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities);
}
}

Expand Down Expand Up @@ -119,10 +122,10 @@ public void CanAddMultiple() {
// Arrange
var _testRepository = new TestRepository(_timeProvider, _currentUserIdProvider);

TestEntity[] listOfItems = {
TestEntity[] listOfItems = [
new(_timeProvider, _currentUserIdProvider) { Id = 42, Label = "toAdd", MasterSystemHierarchy = "001", MasterSystemId = "42" },
new(_timeProvider, _currentUserIdProvider) { Id = 43, Label = "toAdd2", MasterSystemHierarchy = "001", MasterSystemId = "43" }
};
];

// Act
_testRepository.Add(listOfItems);
Expand Down
39 changes: 21 additions & 18 deletions idee5.Common.Data.Tests/ARepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ public TestRepository(AmbientTimeProvider timeProvider, AmbientUserProvider curr

public override void Add(TestEntity item) => TestEntities.Add(item);

public override Task<IEnumerable<TestEntity>> GetAsync(Func<IQueryable<TestEntity>, IQueryable<TestEntity>> func, CancellationToken cancellationToken = default) {
return Task.Factory.StartNew(() => func(TestEntities.AsQueryable()).AsEnumerable(), cancellationToken);
}

public override Task<TResult> GetAsync<TResult>(Func<IQueryable<TestEntity>, TResult> func, CancellationToken cancellationToken = default) {
return Task<TResult>.Factory.StartNew(() => func(TestEntities.AsQueryable()), cancellationToken);
}

public override void Remove(TestEntity item) => TestEntities.Remove(item);

public override Task RemoveAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) => Task.Factory.StartNew(() => TestEntities.RemoveAll(predicate.Compile()));
public override Task RemoveAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
TestEntities.RemoveAll(predicate.Compile());
return Task.CompletedTask;
}

public override void Update(TestEntity item) {
TestEntity listItem = TestEntities.SingleOrDefault(te => te == item);
Expand All @@ -66,18 +61,26 @@ public override Task UpdateOrAddAsync(TestEntity item, CancellationToken cancell
return Task.CompletedTask;
}

public override Task<bool> ExistsAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.Any(predicate);
public override Task<List<TestEntity>> GetAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<List<TestEntity>> GetAllAsync(CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<bool> ExistsAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.Any(predicate.Compile());
return Task.FromResult(result);
}

public override Task<int> CountAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.Count(predicate);
public override Task<int> CountAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.Count(predicate.Compile());
return Task.FromResult(result);
}

public override Task<TestEntity> GetSingleAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.SingleOrDefault(predicate);
public override Task<TestEntity> GetSingleAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.SingleOrDefault(predicate.Compile());
return Task.FromResult(result);
}
}
Expand Down Expand Up @@ -127,10 +130,10 @@ public void CanAddMultiple() {
// Arrange
var _testRepository = new TestRepository(_timeProvider, _currentUserIdProvider);

TestEntity[] listOfItems = {
TestEntity[] listOfItems = [
new TestEntity(_timeProvider, _currentUserIdProvider) { Id = 42, Label = "toAdd", MasterSystemHierarchy = "001", MasterSystemId = "42" },
new TestEntity(_timeProvider, _currentUserIdProvider) { Id = 43, Label = "toAdd2", MasterSystemHierarchy = "001", MasterSystemId = "43" }
};
];

// Act
_testRepository.Add(listOfItems);
Expand Down Expand Up @@ -235,7 +238,7 @@ await _testRepository.GetSingleAsync(e => e.Id == 4)
_testRepository.Update(listOfItems);

// Assert
int result = await _testRepository.GetAsync(q => q.Count()).ConfigureAwait(false);
int result = _testRepository.TestEntities.Count;
Assert.AreEqual(4, result);
Assert.AreEqual(2, listOfItems.Count(i => i.ModifiedBy == "me"));
}
Expand Down
29 changes: 12 additions & 17 deletions idee5.Common.Data.Tests/AuditingRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ public TestRepository(ITimeProvider timeProvider, ICurrentUserIdProvider current
TestEntities = [];
}

public override Task<IEnumerable<AuditedEntity>> GetAsync(Func<IQueryable<AuditedEntity>, IQueryable<AuditedEntity>> func, CancellationToken cancellationToken = default) {
return Task.Run(() => func(TestEntities.AsQueryable()).AsEnumerable(), cancellationToken);
}

public override Task<TResult> GetAsync<TResult>(Func<IQueryable<AuditedEntity>, TResult> func, CancellationToken cancellationToken = default) {
//var target = (AuditedEntity)func.Target.GetType().GetFields()[0].GetValue(func.Target);
//if (target.Id == 2)
// return Task.FromResult(func.Invoke(Array.Empty<AuditedEntity>().AsQueryable()));
//else
// return Task.FromResult(func.Invoke(new List<AuditedEntity> { target }.AsQueryable()));
return Task.Run(() => func(TestEntities.AsQueryable()), cancellationToken);
}

public override void Add(AuditedEntity item) {
base.Add(item); // set the auditing properties
TestEntities.Add(item);
Expand All @@ -56,15 +43,23 @@ public override Task ExecuteAsync(Expression<Func<AuditedEntity, bool>> predicat
throw new NotImplementedException();
}

public override Task<bool> ExistsAsync(Func<AuditedEntity, bool> predicate, CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities.Any(predicate));
public override Task<List<AuditedEntity>> GetAsync(Expression<Func<AuditedEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<List<AuditedEntity>> GetAllAsync(CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<bool> ExistsAsync(Expression<Func<AuditedEntity, bool>> predicate, CancellationToken cancellationToken = default) {
return Task.FromResult(TestEntities.Any(predicate.Compile()));
}

public override Task<int> CountAsync(Func<AuditedEntity, bool> predicate, CancellationToken cancellationToken = default) {
public override Task<int> CountAsync(Expression<Func<AuditedEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public override Task<AuditedEntity> GetSingleAsync(Func<AuditedEntity, bool> predicate, CancellationToken cancellationToken = default) {
public override Task<AuditedEntity> GetSingleAsync(Expression<Func<AuditedEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}
}
Expand Down
34 changes: 21 additions & 13 deletions idee5.Common.Data.Tests/QueryRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -24,21 +25,28 @@ internal class TransientEntity : IEntity {
];

internal class TransientQueryRepository : IQueryRepository<TransientEntity> {
public Task<int> CountAsync(Expression<Func<TransientEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = _transientEntities.Count(predicate.Compile());
return Task.FromResult(result);
}

public Task<int> CountAsync(Func<TransientEntity, bool> predicate, CancellationToken cancellationToken = default) {
return Task.Run(() => _transientEntities.Count(predicate), cancellationToken);
public Task<bool> ExistsAsync(Expression<Func<TransientEntity, bool>> predicate, CancellationToken cancellationToken = default) {
return Task.Run(() => _transientEntities.Any(predicate.Compile()), cancellationToken);
throw new NotImplementedException();
}

public Task<bool> ExistsAsync(Func<TransientEntity, bool> predicate, CancellationToken cancellationToken = default) {
return Task.Run(() => _transientEntities.Any(predicate), cancellationToken);
public Task<List<TransientEntity>> GetAllAsync(CancellationToken cancellationToken = default) {
return Task.FromResult(_transientEntities);
}

public Task<IEnumerable<TransientEntity>> GetAsync(Func<IQueryable<TransientEntity>, IQueryable<TransientEntity>> func, CancellationToken cancellationToken) {
return Task.Run(() => func(_transientEntities.AsQueryable()).AsEnumerable(), cancellationToken);
public Task<List<TransientEntity>> GetAsync(Expression<Func<TransientEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = _transientEntities.Where(predicate.Compile());
return Task.FromResult(result.ToList());
}

public Task<TransientEntity> GetSingleAsync(Func<TransientEntity, bool> expression, CancellationToken cancellationToken = default) {
return Task<TransientEntity>.Factory.StartNew(() => _transientEntities.SingleOrDefault(expression), cancellationToken);
public Task<TransientEntity> GetSingleAsync(Expression<Func<TransientEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = _transientEntities.SingleOrDefault(predicate.Compile());
return Task.FromResult(result);
}
}

Expand Down Expand Up @@ -72,11 +80,11 @@ public async Task CanGetAsync() {
var repo = new TransientQueryRepository();

// Act
IEnumerable<TransientEntity> result = await repo.GetAsync(q => q.Where(e => e.Id == 2), new CancellationToken()).ConfigureAwait(false);
var result = await repo.GetAsync(e => e.Id == 2).ConfigureAwait(false);

// Assert
Assert.AreEqual(expected: 1, actual: result.Count());
Assert.AreEqual(expected: "wl2", actual: result.First().Workload);
Assert.AreEqual(expected: 1, actual: result.Count);
Assert.AreEqual(expected: "wl2", actual: result[0].Workload);
}

[UnitTest, TestMethod]
Expand All @@ -85,10 +93,10 @@ public async Task CanGetAllAsync() {
var repo = new TransientQueryRepository();

// Act
IEnumerable<TransientEntity> result = await repo.GetAsync(e => e, new CancellationToken()).ConfigureAwait(false);
var result = await repo.GetAllAsync().ConfigureAwait(false);

// Assert
Assert.AreEqual(expected: 3, actual: result.Count());
Assert.AreEqual(expected: 3, actual: result.Count);
}

[UnitTest, TestMethod]
Expand Down
25 changes: 12 additions & 13 deletions idee5.Common.Data.Tests/RepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public void Add(IEnumerable<TestEntity> items) {
throw new NotImplementedException();
}

public Task<IEnumerable<TestEntity>> GetAsync(Func<IQueryable<TestEntity>, IQueryable<TestEntity>> func, CancellationToken cancellationToken = default) {
return Task.Run(() => func(TestEntities.AsQueryable()).AsEnumerable(), cancellationToken);
}

public Task RemoveAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) => Task.Run(() => TestEntities.RemoveAll(predicate.Compile()));

public void Remove(TestEntity item) => TestEntities.Remove(item);
Expand All @@ -48,11 +44,6 @@ public void Update(TestEntity item) {
if (listItem != null) listItem = item;
listItem.ModifiedBy = "me";
}

public void Update(Expression<Func<TestEntity, bool>> predicate, Action<TestEntity> action) {
throw new NotImplementedException();
}

public void Update(IEnumerable<TestEntity> items) {
throw new NotImplementedException();
}
Expand All @@ -69,16 +60,24 @@ public Task UpdateOrAddAsync(IEnumerable<TestEntity> items, CancellationToken ca
throw new NotImplementedException();
}

public Task<bool> ExistsAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
public Task<List<TestEntity>> GetAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public Task<List<TestEntity>> GetAllAsync(CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public Task<bool> ExistsAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public Task<int> CountAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
public Task<int> CountAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}

public Task<TestEntity> GetSingleAsync(Func<TestEntity, bool> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.SingleOrDefault(predicate);
public Task<TestEntity> GetSingleAsync(Expression<Func<TestEntity, bool>> predicate, CancellationToken cancellationToken = default) {
var result = TestEntities.SingleOrDefault(predicate.Compile());
return Task.FromResult(result);
}
}
Expand Down
13 changes: 7 additions & 6 deletions idee5.Common.Data/ACompositeKeyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public virtual void Add(IEnumerable<T> items) {
_ = items?.ForEach(Add);
}

/// <inheritdoc />
public abstract Task<IEnumerable<T>> GetAsync(Func<IQueryable<T>, IQueryable<T>> func, CancellationToken cancellationToken = default);

/// <inheritdoc />
public abstract void Remove(T item);

Expand Down Expand Up @@ -58,9 +55,13 @@ public virtual async Task UpdateOrAddAsync(IEnumerable<T> items, CancellationTok
}

/// <inheritdoc />
public abstract Task<bool> ExistsAsync(Func<T, bool> predicate, CancellationToken cancellationToken = default);
public abstract Task<List<T>> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
/// <inheritdoc />
public abstract Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
/// <inheritdoc />
public abstract Task<int> CountAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
/// <inheritdoc />
public abstract Task<int> CountAsync(Func<T, bool> predicate, CancellationToken cancellationToken = default);
public abstract Task<T?> GetSingleAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
/// <inheritdoc />
public abstract Task<T?> GetSingleAsync(Func<T, bool> predicate, CancellationToken cancellationToken = default);
public abstract Task<List<T>> GetAllAsync(CancellationToken cancellationToken = default);
}
Loading

0 comments on commit 2b94a6f

Please sign in to comment.