Exemple #1
0
        public async Task UpdateJobAsync(Job2Dto dto)
        {
            var jobEntity = _dbContext.Job.FirstOrDefault(x => x.Id == dto.Job.JobId);

            _mapper.Map(dto.Job, jobEntity);
            jobEntity.DateModified = DateTime.UtcNow;
            jobEntity.ModifiedBy   = GetCurrentUserEmail();

            JobCreatedIntegrationEvent jobUpdatedEvent = null;

            //TODO: Verify when we change the BillTo from Vendor to Account.. bill to Type is being changed.
            await ResilientTransaction.New(_dbContext).ExecuteAsync(async() =>
            {
                _dbContext.Job.Update(jobEntity);
                await _dbContext.SaveChangesAsync();

                UpdateTransferee(dto);
                UpdatePhone(dto);
                UpdateTransfereeEmail(dto);
                UpdateAddress(dto);

                jobUpdatedEvent        = jobEntity.FromJob <JobCreatedIntegrationEvent>();
                jobUpdatedEvent.UserId = GetCurrentUserEmail();

                await _eventLogService.SaveEventAsync(jobUpdatedEvent);
            });

            // Publish the integration event through the event bus
            _eventBus.Publish(jobUpdatedEvent);
            await _eventLogService.MarkEventAsPublishedAsync(jobUpdatedEvent.Id);

            _logger.LogInformation("Job object updated");
        }
Exemple #2
0
 public async Task AddAndSaveEventAsync(IntegrationEvent @event)
 {
     await ResilientTransaction.CreateNew(catalogDbContext).ExecuteAsync(async() => {
         await catalogDbContext.SaveChangesAsync();
         await eventLogService.SaveEventAsync(@event, catalogDbContext.Database.CurrentTransaction);
     });
 }
Exemple #3
0
 public async Task SaveEventAsync(IntegrationEvent evt)
 {
     await ResilientTransaction.New(_context)
     .ExecuteAsync(async() => {
         await _eventLogService.SaveEventAsync(evt, _context.Database.CurrentTransaction.GetDbTransaction());
     });
 }
 public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
 {
     await ResilientTransaction.New(_catalogContext).ExecuteAsync(async() =>
     {
         await _catalogContext.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction);
     });
 }
Exemple #5
0
 private async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt)
 {
     await ResilientTransaction.New(_productContext)
     .ExecuteAsync(async() => {
         await _productContext.SaveChangesAsync();
         await _integrationEventLogService.SaveEventAsync(evt, _productContext.Database.CurrentTransaction.GetDbTransaction());
     });
 }
Exemple #6
0
 public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent @event)
 {
     await ResilientTransaction.New(_catalogContext).ExecuteAsync(async() =>
     {
         _logger.Information($"SaveEventAndCatalogContextChangesAsync  {@event}");
         await _catalogContext.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(@event, _catalogContext.Database.CurrentTransaction);
     });
 }
Exemple #7
0
 public async Task SaveEventAndInventoryContextChangesAsync(IntegrationEvent evt)
 {
     await ResilientTransaction.New(_inventoryContext)
     .ExecuteAsync(async() =>
     {
         await _inventoryContext.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(evt, DbContextTransactionExtensions.GetDbTransaction(_inventoryContext.Database.CurrentTransaction));
     });
 }
Exemple #8
0
        public async Task SaveApplicationContextAndEventLogsContextChangesAsync()
        {
            var resilientTransaction = ResilientTransaction.Create(_dbContext);

            await resilientTransaction.ExecuteAsync(async() =>
            {
                await _dbContext.SaveChangesAsync();
                await _integrationEventLogContext.SaveChangesAsync();
            });
        }
Exemple #9
0
 public async Task SaveEventAndOrderingContextChangesAsync()
 {
     //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
     //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
     await ResilientTransaction.New(_ordersContext)
     .ExecuteAsync(async() => {
         // Achieving atomicity between original ordering database operation and the IntegrationEventLog thanks to a local transaction
         await _ordersContext.SaveChangesAsync();
     });
 }
Exemple #10
0
        public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
        {
            _logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);

            await ResilientTransaction.New(_gestaoNormasDbContext).ExecuteAsync(async() =>
            {
                await _gestaoNormasDbContext.SaveChangesAsync();
                await _eventLogService.SaveEventAsync(evt, _gestaoNormasDbContext.Database.CurrentTransaction);
            });
        }
Exemple #11
0
        public async Task SaveChangesIncludingEventLogAsync(IntegrationEvent @event)
        {
            _logger.LogInformation("Saving changes and integrationEvent: {IntegrationEventId} from {appName}", @event.Id, _appName);

            await ResilientTransaction.New(_context).ExecuteAsync(async() =>
            {
                await _context.SaveChangesAsync();
                await _integrationEventLogRepository.SaveEventAsync(@event, _context.Database.CurrentTransaction);
            });
        }
Exemple #12
0
        public async Task <int> AddJobAsync(Job2Dto job2Dto)
        {
            _logger.LogInformation("Incoming Dto {0}", job2Dto);
            string currentUsername = GetCurrentUserEmail();

            var job = _mapper.Map <Job>(job2Dto.Job);

            if (job != null)
            {
                JobCreatedIntegrationEvent jobCreatedEvent = null;

                await ResilientTransaction.New(_dbContext).ExecuteAsync(async() =>
                {
                    job.TransfereeId  = SaveTransferee(job2Dto.Transferee);
                    job.AccountEntity = _dbContext.AccountEntity.FirstOrDefault(a => a.Id == job.AccountEntityId);

                    var dateTimeAdded = DateTime.UtcNow;
                    job.DateCreated   = dateTimeAdded;
                    job.DateModified  = dateTimeAdded;
                    job.CreatedBy     = GetCurrentUserEmail();
                    job.ModifiedBy    = GetCurrentUserEmail();

                    if (job2Dto.Job.IsSurveyAndBid)
                    {
                        job.JobStatus = JobStatusIdentifier.SURVEY_BID;
                    }

                    _dbContext.Job.Attach(job);
                    _dbContext.Entry(job).State = EntityState.Added;

                    await _dbContext.SaveChangesAsync();

                    //update the fks in the job table
                    var kvp                  = await SaveAddresses(job2Dto.JobInfo);
                    job.OriginAddressId      = kvp.FirstOrDefault(s => s.Key == AddressType.ORIGIN).Value;
                    job.DestinationAddressId = kvp.FirstOrDefault(s => s.Key == AddressType.DESTINATION).Value;

                    await _dbContext.SaveChangesAsync();

                    jobCreatedEvent            = job.FromJob <JobCreatedIntegrationEvent>();
                    jobCreatedEvent.Transferee = job2Dto.ToTransfereeIntegrationEvent <TransfereeCreatedIntegrationEvent>();
                    jobCreatedEvent.UserId     = GetJobsMoveConsultantEmail(job.Id);

                    await _eventLogService.SaveEventAsync(jobCreatedEvent);
                });

                // Publish the integration event through the event bus
                _eventBus.Publish(jobCreatedEvent);
                await _eventLogService.MarkEventAsPublishedAsync(jobCreatedEvent.Id);

                _logger.LogInformation("Job added {0}", job.Id);
            }

            return(job.Id);
        }
 private async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent @event)
 {
     //说明:不同微服务之间,要保证业务操作和事件发布之间的原子性,本来需采用分布式(XA)事务,这违背了微服务架构追求最终一致性的原则;
     //采用事件表的方式,通过一个本地事务,保证业务操作和事件发布之间的原子性;然后,在一个单独的任务中读取事件表中未发布的事件,再将事件发布到消息中间件中。
     await ResilientTransaction.New(this._orderingContext)
     .ExecuteAsync(async() =>
     {
         await this._orderingContext.SaveChangesAsync();
         await this._eventLogService.SaveEventAsync(@event, this._orderingContext.Database.CurrentTransaction.GetDbTransaction());
     });
 }
        public async Task AddAndSaveEventAsync(IntegrationEvent evt)
        {
            _logger.LogInformation("----- IdentityIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);

            await ResilientTransaction.New((DbContext)_customIdentityDbContext).ExecuteAsync(async() =>
            {
                // Achieving atomicity between original database operation and the IntegrationEventLog thanks to a local transaction
                await _customIdentityDbContext.SaveChangesAsync();
                await _eventLogService.SaveEventAsync(evt, _customIdentityDbContext.Database.CurrentTransaction);
            });
        }
Exemple #15
0
 private async Task SaveEventAndBusinessDbContextChangesAsync(IEvent evt)
 {
     //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
     //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
     await ResilientTransaction.New(_context)
     .ExecuteAsync(async() => {
         // Achieving atomicity between original ordering database operation and the IntegrationEventLog thanks to a local transaction
         await _context.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(evt, _context.Database.CurrentTransaction.GetDbTransaction());
     });
 }
 private async Task Save(Payload <IPayload> payload)
 {
     await ResilientTransaction.New(_context).ExecuteAsync(async() =>
     {
         _context.Apartment.Add(payload.Apartment);
         await _context.SaveChangesAsync();
         payload.InObject.ApartmentId = payload.Apartment.Id;
         _context.Add(payload.InObject);
         await _context.SaveChangesAsync();
     });
 }
Exemple #17
0
 public async Task SaveEventAndMovieContextChangesAsync(Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events.IntegrationEvent evt)
 {
     //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
     //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
     await ResilientTransaction.New(_movieContext)
     .ExecuteAsync(async() =>
     {
         // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
         await _movieContext.SaveChangesAsync();
         await _eventLogService.SaveEventAsync(evt, _movieContext.Database.CurrentTransaction.GetDbTransaction());
     });
 }
        public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
        {
            _logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);

            //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
            //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
            await ResilientTransaction.New(_catalogContext).ExecuteAsync(async() => {
                // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
                await _catalogContext.SaveChangesAsync();
                await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction());
            });
        }
 public async Task SaveEventAndProductContextChangesAsync(IntegrationEvent @event, CancellationToken cancellationToken = default(CancellationToken))
 {
     //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
     //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
     await ResilientTransaction.New(_salesContext)
     .ExecuteAsync(async() =>
     {
         // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
         await _salesContext.SaveChangesAsync(cancellationToken);
         await _eventLogService.SaveEventAsync(@event, _salesContext.Database.CurrentTransaction.GetDbTransaction());
     });
 }
Exemple #20
0
        private static async Task Main()
        {
            var mapper               = new Mapper();
            var dbContext            = new DispatcherDbContext();
            var unitOfWork           = new Data.EF.UnitOfWork(dbContext, mapper);
            var resilientTransaction = new ResilientTransaction <DispatcherDbContext>(() => dbContext);
            var orderService         = new OrderService(resilientTransaction, unitOfWork);

            await dbContext.Database.EnsureCreatedAsync();

            await orderService.RunAsync();
        }
        public Task RemoveFolders(IReadOnlyCollection <FolderIdentity> folderIds, CancellationToken cancellationToken)
        {
            return(ResilientTransaction.New(context).ExecuteAsync(async() =>
            {
                foreach (var folderId in folderIds)
                {
                    await RemoveFolder(await context.Folders.FindAsync(
                                           new object[] { folderId.Id, folderId.UserIdentity }, cancellationToken), cancellationToken);
                }

                await context.SaveChangesAsync(cancellationToken);
            }));
        }
Exemple #22
0
        public async Task SaveEventsAndUsersContextChangesAsync(params IntegrationEvent[] events)
        {
            _logger.LogInformation("----- (Users.API) UsersIntegrationEventService - Saving changes and integrationEvents: {IntegrationEventsIds}",
                                   string.Join(", ", events.Select(evt => evt.Id)));

            //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
            //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
            await ResilientTransaction.New(_usersContext).ExecuteAsync(async() =>
            {
                // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
                await _usersContext.SaveChangesAsync();
                await _eventLogService.SaveEventsAsync(events, _usersContext.Database.CurrentTransaction);
            });
        }
Exemple #23
0
        public async Task SaveEventAndDbContextChangesAsync(IEnumerable <Event> events)
        {
            _logger.LogInformation("----- EventService - Saving changes and multiple events: {EventId}");

            //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
            //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
            await ResilientTransaction.New(_context).ExecuteAsync(async() =>
            {
                // Achieving atomicity between original database operation and the EventLog thanks to a local transaction
                await _context.SaveChangesAsync();
                foreach (var @event in events)
                {
                    await _eventLogService.SaveEventAsync(@event, _context.Database.CurrentTransaction);
                }
            });
        }
Exemple #24
0
        private async Task UpdateStateInternal(DocumentInfo document, InProcessDocumentState newState, string errorInfo,
                                               CancellationToken cancellationToken)
        {
            InProgressDocumentDbItem inProgressDocument = null;
            await ResilientTransaction.New(context).ExecuteAsync(async() =>
            {
                var(id, userIdentity) = document.Id;
                inProgressDocument    =
                    await context.InProgressDocuments.FindAsync(new object[] { id, userIdentity }, cancellationToken);
                if (inProgressDocument != null)
                {
                    context.InProgressDocuments.Update(inProgressDocument);
                }
                else
                {
                    inProgressDocument = new InProgressDocumentDbItem
                    {
                        Id           = id,
                        UserIdentity = userIdentity,
                    };
                    context.InProgressDocuments.Add(inProgressDocument);
                }

                inProgressDocument.ErrorInfo            = errorInfo;
                inProgressDocument.FileName             = document.FileName;
                inProgressDocument.State                = newState;
                inProgressDocument.LastStatusUpdateTime = DateTimeOffset.UtcNow;

                await context.SaveChangesAsync(cancellationToken);
            });

            if (inProgressDocument.State == newState)
            {
                await mediator.Publish(
                    new InProgressDocumentStateChanged(
                        new InProgressDocument(
                            document.Id,
                            inProgressDocument.FileName, inProgressDocument.State,
                            inProgressDocument.LastStatusUpdateTime, inProgressDocument.ErrorInfo)),
                    cancellationToken);
            }
        }
Exemple #25
0
        public async Task SaveEventAndChangesAsync(IntegrationEvent evt)
        {
            _logger.LogInformation("----- POSIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);
            await ResilientTransaction.New(_posContext, _logger).ExecuteAsync(async() =>
            {
                // Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
                try
                {
                    if (await _posContext.SaveChangesAsync() <= 0)
                    {
                        _logger.LogError("----- POSIntegrationEventService {0}", "Not can saved in Db");
                    }
                }
                catch (DbUpdateException e)
                {
                    _logger.LogError("***** Error In SaveChangesAsync Process {0}", e.Message);
                }

                await _eventLogService.SaveEventAsync(@event: evt, transaction: Guid.NewGuid());
            });
        }
Exemple #26
0
 public async Task <T> CreateTransactionAsync <T> (Func <Task <T> > action, CancellationToken ct)
 {
     return(await ResilientTransaction.New(_context).ExecuteAsync(action, ct));
 }