Exemplo n.º 1
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _uow.Dispose();
            }

            base.Dispose(disposing);
        }
Exemplo n.º 2
0
        public async Task <DomainEvent[]> ProcessWithEvents(TCommand command, CrossContext ctx)
        {
            var  logger = _loggerFactory.CreateLogger($"CommandProcessor[{nameof(command)}][{ctx.CorrelationUuid}]");
            IUow uow    = null;

            try
            {
                logger.LogInformation($"Started at {DateTime.Now}");
                var handler = _handlerBuilder();
                handler.Logger = _loggerFactory.CreateLogger(nameof(handler));
                handler.ExternalServiceProvider = _externalServiceProvider;
                handler.CancellationToken       = ctx.Token;

                await handler.BeforeExecute(async func =>
                {
                    using (var nonTransactionalUow = _uowfactory.CreateUnitOfWork(null, ctx.Token))
                    {
                        await func(nonTransactionalUow);
                    }
                }, command);

                var transactionUuid = UseTransaction ? Guid.NewGuid() : (Guid?)null;
                uow = _uowfactory.CreateUnitOfWork(transactionUuid, ctx.Token);

                await handler.Execute(uow, command);

                await uow.Commit();

                return(handler.EventList.ToArray());
            }
            catch (Exception e)
            {
                var t = uow?.Rollback();
                if (t != null)
                {
                    await t;
                }

                logger.LogError("{0}\n{1}", e.Message, e.StackTrace);
                throw;
            }
            finally
            {
                uow?.Dispose();

                logger.LogInformation($"Done at {DateTime.Now}");
            }
        }