Exemple #1
0
        /// <summary>
        /// Commits the changes within the persistence context.
        /// </summary>
        /// <param name="transactionScope">The transaction scope.</param>
        /// <returns>IServiceResponse{Boolean}.</returns>
        protected override IServiceResponse <Unit> CommitTransaction(TransactionScope transactionScope)
        {
            if (Parent == null)
            {
                try
                {
                    using (transactionScope)
                    {
                        return(CommitTransactionInternal()
                               .Catch(errors =>
                        {
                            // TODO: (DG) Support exceptions!
                            // If NContext exception vs error handling IS Exception-based, don't rollback here; just throw;
                            Rollback();
                            // throw new NContextPersistenceException(errors);
                        })
                               .Let(_ =>
                        {
                            transactionScope.Complete();
                        }));
                    }
                }
                catch (Exception exception)
                {
                    Rollback();

                    // TODO: (DG) NContext Exception vs ErrorHandling
                    return(ErrorBaseExtensions.ToServiceResponse(NContextPersistenceError.CommitFailed(Id, "tranId", new AggregateException(exception))));
                }
            }

            return(CommitTransactionInternal());
        }
        protected virtual void Validate(HttpActionContext actionContext, IEnumerable <HttpParameterDescriptor> parameterDescriptors)
        {
            Contract.Requires(actionContext != null);
            Contract.Requires(parameterDescriptors != null && parameterDescriptors.Any());

            var requiredParameterDescriptors =
                parameterDescriptors.Where(
                    descriptor =>
                    !descriptor.IsOptional &&
                    (descriptor.ParameterType.IsPrimitive || descriptor.ParameterBinderAttribute is ModelBinderAttribute));

            var requiredBodyParameterDescriptor    = actionContext.ActionDescriptor.ActionBinding.ParameterBindings.Single(binding => binding.WillReadBody).Descriptor;
            var requiredBodyParameterArgumentValue = actionContext.ActionArguments[requiredBodyParameterDescriptor.ParameterName];
            var requiredBodyProperties             = TypeDescriptor.GetProperties(requiredBodyParameterArgumentValue).Cast <PropertyDescriptor>().ToList();

            foreach (var parameterDescriptor in requiredParameterDescriptors)
            {
                var requiredParameterValue = actionContext.ActionArguments[parameterDescriptor.ParameterName];
                var bodyProperty           = requiredBodyProperties.SingleOrDefault(propertyDescriptor => IsMatch(parameterDescriptor, propertyDescriptor));
                if (bodyProperty == null)
                {
                    if (BodyParameterNotFoundReturnsError)
                    {
                        actionContext.Response =
                            ErrorBaseExtensions.ToServiceResponse <Object>(
                                HttpParameterValidationError.RequiredParameterNotFoundInBody(parameterDescriptor.ParameterType, parameterDescriptor.ParameterName))
                            .ToHttpResponseMessage(actionContext.Request);
                        return;
                    }

                    continue;
                }

                var bodyPropertyValue = bodyProperty.GetValue(requiredBodyParameterArgumentValue);
                if (bodyPropertyValue == null || !bodyPropertyValue.Equals(requiredParameterValue))
                {
                    actionContext.Response =
                        ErrorBaseExtensions.ToServiceResponse <Object>(
                            HttpParameterValidationError.ValidationFailed(parameterDescriptor.ParameterName, requiredParameterValue, bodyPropertyValue))
                        .ToHttpResponseMessage(actionContext.Request);
                    return;
                }
            }
        }