Example #1
0
        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);
        }