public async Task <int> CommitAsync(
            CancellationToken cancellationToken = default,
            Guid?internalCommandId = null)
        {
            await _domainEventsDispatcher.DispatchEventsAsync();

            var options = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.ReadCommitted
            };

            using var transaction = new TransactionScope(TransactionScopeOption.Required, options, TransactionScopeAsyncFlowOption.Enabled);

            await _aggregateStore.Save();

            await _outbox.Save();

            if (internalCommandId.HasValue)
            {
                using var connection = _sqlConnectionFactory.CreateNewConnection();
                await connection.ExecuteScalarAsync("UPDATE payments.InternalCommands " +
                                                    "SET ProcessedDate = @Date " +
                                                    "WHERE Id = @Id",
                                                    new
                {
                    Date = DateTime.UtcNow,
                    Id   = internalCommandId.Value
                });
            }

            transaction.Complete();

            return(0);
        }
Ejemplo n.º 2
0
        public async Task <int> CommitAsync(
            CancellationToken cancellationToken = default,
            Guid?internalCommandId = null)
        {
            await _domainEventsDispatcher.DispatchEventsAsync();

            return(await _context.SaveChangesAsync(cancellationToken));
        }
        public async Task <TResponse> Handle(T command, CancellationToken cancellationToken)
        {
            var r = await this._decorated.Handle(command, cancellationToken);

            //_db.SaveChanges();
            await _domainEventsDispatcher.DispatchEventsAsync();

            return(r);
        }
        public async Task Add(Calendar calendar)
        {
            var connection = _sqlConnectionFactory.GetOpenConnection();
            var exist      = await GetById(calendar.Id);

            if (exist == null)
            {
                var procedure = "[InserCalendarIfDoesntExist]";
                await connection.ExecuteAsync(procedure, calendar.CreateNew(), commandType : CommandType.StoredProcedure);

                await _domainEventsDispatcher.DispatchEventsAsync(calendar.GetDomainEvents());
            }

            if (calendar.UsersCount() > 0)
            {
                await Iterate(calendar.GetCalendarUsers());
            }
        }
Ejemplo n.º 5
0
        public async Task <int> CommitAsync(CancellationToken cancellationToken = default)
        {
            var domainEntities = ChangeTracker.Entries <Entity>()
                                 .Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any())
                                 .Select(x => x.Entity);

            await eventsDispatcher.DispatchEventsAsync(domainEntities);

            return(await base.SaveChangesAsync(cancellationToken));
        }
Ejemplo n.º 6
0
        public async Task <int> CommitAsync(CancellationToken cancellationToken = default)
        {
            var notifications = await _domainEventsDispatcher.DispatchEventsAsync(this);

            var saveResult = await base.SaveChangesAsync(cancellationToken);

            var tasks = notifications
                        .Select(async(notification) =>
            {
                await _mediator.Publish(notification, cancellationToken);
            });

            await Task.WhenAll(tasks);

            return(saveResult);
        }
Ejemplo n.º 7
0
        public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
                                                          CancellationToken cancellationToken = new CancellationToken())
        {
            SetTimestampFields();

            var notifications = await _domainEventsDispatcher.DispatchEventsAsync(this).ConfigureAwait(false);

            var saveResult = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);

            var tasks = notifications
                        .Select(async(notification) =>
            {
                await _mediator.Publish(notification, cancellationToken);
            });

            await Task.WhenAll(tasks);

            return(saveResult);
        }
Ejemplo n.º 8
0
        public async Task CommitAsync(CancellationToken cancellationToken = default)
        {
            try
            {
                _transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken);

                await _dbContext.SaveChangesAsync(cancellationToken);

                //committing before dispatching
                await _transaction.CommitAsync(cancellationToken);

                //we publish here events which are not requiring committing transaction in accordance with DDD best practice
                await _domainEventsDispatcher.DispatchEventsAsync();
            }
            catch (Exception)
            {
                await RollbackAsync(cancellationToken);

                throw;
            }
        }
        public async Task Handle(T notification, CancellationToken cancellationToken)
        {
            await _decorated.Handle(notification, cancellationToken);

            await _dispatcher.DispatchEventsAsync();
        }
Ejemplo n.º 10
0
        public async Task Commit()
        {
            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            await _domainEventsDispatcher.DispatchEventsAsync();
        }
Ejemplo n.º 11
0
        public async Task <int> CommitAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await _domainEventsDispatcher.DispatchEventsAsync();

            return(await _employeesContext.SaveChangesAsync(cancellationToken));
        }