protected async Task <OperationResultOf <TEntity> > InternalTryGetSingleAsync( DbContext dbSession, TKey key, bool trackChanges, CancellationToken cancellation, params Expression <Func <TEntity, object> >[] toBeIncluded) { var query = trackChanges ? dbSession.Set <TEntity>().AppendIncludeExpressions(toBeIncluded) : dbSession.Set <TEntity>().AsNoTracking().AppendIncludeExpressions(toBeIncluded); var buildKeyPredicateOpRes = PrimaryKeyExpressionBuilder.TryBuildForSingleKey <TKey, TEntity>(dbSession, key); if (!buildKeyPredicateOpRes) { return(buildKeyPredicateOpRes.ErrorMessage.AsFailedOpResOf <TEntity>()); } var filterExpression = buildKeyPredicateOpRes.Value; var foundEntity = await query.SingleOrDefaultAsync(filterExpression, cancellation) .ConfigureAwait(false); var success = foundEntity != null; return(success ? foundEntity.AsSuccessfulOpRes() : $"Failed to find '{key}' matching entity'".AsFailedOpResOf <TEntity>()); }
public void ValidSingleValueTypeKey_TryBuildForSingleKey_SuccessfulyBuilt() { //Arrange var builder = new PrimaryKeyExpressionBuilder(); using var ctx = new MyDbContext(); //Act var buildKeyPredicteOpres = builder.TryBuildForSingleKey <int, SingleValueTypeKeyEntity>(ctx, key: 1); //Assert Assert.IsTrue(buildKeyPredicteOpres, buildKeyPredicteOpres.ErrorMessage); }
public void ValidCompositValueTypeKeys_TryBuildForMultitypeCompositeKey_SuccessfulyBuilt() { //Arrange var builder = new PrimaryKeyExpressionBuilder(); using var ctx = new MyDbContext(); //Act var buildKeyPredicteOpres = builder.TryBuildForMultiTypeCompositeKey <SameValueTypeComposedKeysEntity>( ctx, compositeKeyParts: new object[] { 1, 2, 3 }); //Assert Assert.IsTrue(buildKeyPredicteOpres, buildKeyPredicteOpres.ErrorMessage); }
public void InvalidTypeOfCompositValueTypeKeys_TryBuildForMultitypeCompositeKey_BuiltFailure() { //Arrange var builder = new PrimaryKeyExpressionBuilder(); using var ctx = new MyDbContext(); //Act var buildKeyPredicteOpres = builder.TryBuildForMultiTypeCompositeKey <SameValueTypeComposedKeysEntity>( ctx, compositeKeyParts: new object[] { 1, 2, "3" }); //Assert Assert.IsFalse(buildKeyPredicteOpres, buildKeyPredicteOpres.ErrorMessage); }
public void ValidCompositRefTypeKeys_TryBuildForCompositeKey_SuccessfulyBuilt() { //Arrange var builder = new PrimaryKeyExpressionBuilder(); using var ctx = new MyDbContext(); //Act var buildKeyPredicteOpres = builder .TryBuildForCompositeKey <string, SameRefTypeKeysComposedEntity>( ctx, compostiteKeyParts: new[] { "1", "2", "3" }); //Assert Assert.IsTrue(buildKeyPredicteOpres, buildKeyPredicteOpres.ErrorMessage); }