示例#1
0
        /// <summary>
        ///   Retrieves an order from pending storage.
        /// </summary>
        ///
        /// <param name="log">The logging instance to use for emitting information.</param>
        /// <param name="storage">The storage to use for the order.</param>
        /// <param name="serializerSettings">The settings to use for JSON serialization operations.</param>
        /// <param name="partner">The partner associated with the order.</param>
        /// <param name="orderId">The unique identifier of the order to retrieve the detials of.</param>
        /// <param name="correlationId">An optional identifier used to correlate activities across the disparate parts of processing, including external interations.</param>
        /// <param name="emulatedResult">An optional emulated result to use in place of interacting with storage.</param>
        ///
        /// <returns>The result of the operation.</returns>
        ///
        protected virtual async Task <OperationResult <CreateOrderMessage> > RetrievePendingOrderAsync(ILogger log,
                                                                                                       IOrderStorage storage,
                                                                                                       JsonSerializerSettings serializerSettings,
                                                                                                       string partner,
                                                                                                       string orderId,
                                                                                                       string correlationId           = null,
                                                                                                       OperationResult emulatedResult = null)
        {
            OperationResult <CreateOrderMessage> result;

            try
            {
                if (emulatedResult != null)
                {
                    result = new OperationResult <CreateOrderMessage>
                    {
                        Outcome     = emulatedResult.Outcome,
                        Reason      = emulatedResult.Reason,
                        Recoverable = emulatedResult.Recoverable,
                        Payload     = (String.IsNullOrEmpty(emulatedResult.Payload)) ? null : JsonConvert.DeserializeObject <CreateOrderMessage>(emulatedResult.Payload, serializerSettings)
                    };
                }
                else
                {
                    var storageResult = await storage.TryRetrievePendingOrderAsync(partner, orderId);

                    if (!storageResult.Found)
                    {
                        log.Error("Order details for {Partner}//{Order} were not found in the pending order storage. Submission cannot continue. Emulated: {Emulated}.",
                                  partner,
                                  orderId,
                                  (emulatedResult != null));

                        return(new OperationResult <CreateOrderMessage>
                        {
                            Outcome = Outcome.Failure,
                            Reason = FailureReason.OrderNotFoundInPendingStorage,
                            Recoverable = Recoverability.Final,
                        });
                    }

                    result = new OperationResult <CreateOrderMessage>
                    {
                        Outcome     = Outcome.Success,
                        Reason      = String.Empty,
                        Recoverable = Recoverability.Final,
                        Payload     = storageResult.Order
                    };
                }

                log.Information("The order for {Partner}//{Order} was retrieved from pending storage.  Emulated: {Emulated}.  Result: {Result}",
                                partner,
                                orderId,
                                (emulatedResult != null),
                                result);
            }

            catch (Exception ex)
            {
                log.Error(ex, "An error occured while retrieving {Partner}//{Order} as pending submission.", partner, orderId);
                return(OperationResult <CreateOrderMessage> .ExceptionResult);
            }

            return(result);
        }