Ejemplo n.º 1
0
        public async Task <CreateOrderResponse> CreateOrderAsync(CreateOrderRequest request)
        {
            var result = new CreateOrderResponse();
            var errors = Enumerable.Empty <string>() as ICollection <string>;

            try
            {
                var spec = new ValidCreateOrderRequestSpecification();
                if (spec.IsSatisfiedBy(request, ref errors))
                {
                    await this.dataGateway.InsertOrderAsync(request.AsEntity());
                }
                else
                {
                    result.SetError(Errors.CreateValidationError, errors);
                }
            }
            catch (Exception ex)
            {
                this.logger.WriteException(ex);
                throw new OrderServiceException(ex);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <CreateOrderResponse> CreateOrderAsync(CreateOrderRequest request)
        {
            var result = new CreateOrderResponse();
            ICollection <string> errors = new Collection <string>();

            try
            {
                //// Validate using Specification classes. You can leverage factories to inject
                //// your specifications if it touches the database
                var spec = new ValidCreateOrderRequestSpecification();
                if (spec.IsSatisfiedBy(request, ref errors))
                {
                    //// Decouple Service Models from Shared models i.e. Create `AsEntity` extension to convert vice-versa
                    await this.dataGateway.InsertOrderAsync(request.AsEntity());
                }
                else
                {
                    //// Communicate Specification-added errors, and return appropriate error.
                    result.SetError(Errors.CreateValidationError, errors);
                }
            }
            catch (Exception ex)
            {
                //// Log and Wrap exception then rethrow
                this.logger.WriteException(ex);
                throw new OrderServiceException(ex);
            }

            return(result);
        }