public UnitOfWorkOptions Build <TResponse>(IRequest <TResponse> request) { IsolationLevel isolationLevel; switch (request) { case ICommand <TResponse> _: case IQuery <TResponse> _: case ICommand _: _logger.Debug("Getting isolation level for Command or Query"); isolationLevel = IsolationLevel.ReadCommitted; break; case IQueryUncommitted <TResponse> _: _logger.Debug("Getting isolation level for QueryUncommitted"); isolationLevel = IsolationLevel.ReadUncommitted; break; default: throw new Exception("Request should implement ICommand<> or IQuery<> to enable Unit of Work."); } return(new UnitOfWorkOptions { IsolationLevel = isolationLevel }); }
public async Task DispatchAsync(ICqrsEvent @event, CancellationToken cancellationToken = default) { _logger.Debug($"Publishing event to bus from EventDispatcher. Id: {@event.Id} SourceType: {@event.SourceTypeName}"); await _bus.PublishAsync(@event, cancellationToken); _logger.Debug("Published event"); }
private async Task DispatchEventsToBeProcessed(CancellationToken cancellationToken) { var eventId = Guid.Empty; try { _logger.Debug("About to dispatch EventsToBeProcessed..."); foreach (var @event in _eventBag.EventsToBeProcessed.ToList()) { eventId = @event.Id; _logger.Debug($"Publishing event. Id: {@event.Id} SourceType: {@event.SourceTypeName}"); await _eventDispatcher.DispatchAsync(@event, cancellationToken); _eventBag.EventsToBeProcessed.Remove(@event); if (_cqrsOptions.PurgeEventsToBeProcessed) { await PurgeEvent(@event, cancellationToken); } } } catch (Exception ex) { _logger.Error($"Dispatching failed. Id: {eventId}"); _logger.Error(ex); } }
public async Task PublishAsync <TRequest>(TRequest message, CancellationToken cancellationToken = default) where TRequest : IRequest { _logger.Debug($"Publishing message of type - {message.GetType().Name} ..."); await _busControl.Publish(message, cancellationToken).ConfigureAwait(false); _logger.Debug("Message published"); }
public async Task <TResponse> RunAsync <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken, Func <TRequest, CancellationToken, Task <TResponse> > next) where TRequest : IRequest <TResponse> { _logger.Debug("TestInterceptor Started"); var response = await next.Invoke(request, cancellationToken); _logger.Debug("TestInterceptor Ended"); return(response); }
public override async Task <TResponse> ExecuteAsync <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken, Func <TRequest, CancellationToken, Task <TResponse> > next) { _logger.Debug("TestRequestSpecificInterceptor Started"); var response = await next.Invoke(request, cancellationToken); _logger.Debug("TestRequestSpecificInterceptor Ended"); return(response); }
public override TResponse Execute <TRequest, TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> next) { _logger.Debug($"TestRequestSpecificInterceptor Started"); var response = next.Invoke(request); _logger.Debug($"TestRequestSpecificInterceptor Ended"); return(response); }
public async Task Consume(ConsumeContext <TRequest> context) { var request = context.Message; _logger.Debug($"Message of type {request.GetType().Name} consumer. " + "Sending to mediator..."); await _mediator.ProcessAsync(request, context.CancellationToken); _logger.Debug("Message sent to Mediator"); }
public TResponse Run <TRequest, TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> next) where TRequest : IRequest <TResponse> { _logger.Debug("TestInterceptor Started"); var response = next.Invoke(request); _logger.Debug("TestInterceptor Ended"); return(response); }
public async Task <TResponse> RunAsync <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken, Func <TRequest, CancellationToken, Task <TResponse> > next) where TRequest : IRequest <TResponse> { _logger.Debug($"Entering {nameof(ChangeTrackerInterceptor)}..."); _changeTrackerContext.IsQueryRequest = request is IQuery <TResponse>; _logger.Debug($"IsQueryRequest: {_changeTrackerContext.IsQueryRequest}"); var response = await next(request, cancellationToken); return(response); }
public TResponse Run <TRequest, TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> next) where TRequest : IRequest <TResponse> { _logger.Debug($"Entering {nameof(EFQueryTrackingBehaviorInterceptor)}..."); _mediatorDataContext.IsQueryRequest = request is IQuery <TResponse> || request is IStaleQuery <TResponse>; _logger.Debug($"IsQueryRequest: {_mediatorDataContext.IsQueryRequest}"); var response = next(request); return(response); }
public override TResponse Execute <TRequest, TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> next) { var stopwatch = new Stopwatch(); _logger.Debug($"StopwatchInterceptor started at {_boltOnClock.Now}"); var response = next(request); _logger.Debug($"StopwatchInterceptor ended at {_boltOnClock.Now}. Time elapsed: {stopwatch.ElapsedMilliseconds}"); return(response); }
public override async Task <TResponse> ExecuteAsync <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken, Func <TRequest, CancellationToken, Task <TResponse> > next) { var stopwatch = new Stopwatch(); _logger.Debug($"StopwatchInterceptor started at {_boltOnClock.Now}"); var response = await next(request, cancellationToken); _logger.Debug($"StopwatchInterceptor ended at {_boltOnClock.Now}. Time elapsed: {stopwatch.ElapsedMilliseconds}"); return(response); }
public void Run() { _logger.Debug("Cleaning up bus..."); var busControl = _serviceProvider.GetService <IBusControl>(); if (busControl == null) { return; } _logger.Debug("Stopping bus..."); busControl.Stop(); _logger.Debug("Stopped bus"); }
public async Task HandleAsync(StudentUpdatedEvent request, CancellationToken cancellationToken) { _logger.Debug($"{nameof(StudentUpdatedEventHandler)} invoked"); var studentFlattened = await _repository.GetByIdAsync(request.SourceId); var isSuccessful = studentFlattened.UpdateInput(request); if (isSuccessful) { _logger.Debug($"{nameof(StudentFlattened)} updated. " + $"Input1: {studentFlattened.FirstName} Input2Property1: {studentFlattened.Input2Property1} " + $"Input2Propert2: {studentFlattened.Input2Property2}"); // comment the below line to avoid test warning, as TestCqrsUpdateEvent gets added in two different entities await _repository.UpdateAsync(studentFlattened); } }
public async Task HandleAsync(UpdateStudentRequest request, CancellationToken cancellationToken) { _logger.Debug($"{nameof(UpdateStudentHandler)} invoked"); var student = await _repository.GetByIdAsync(CqrsConstants.EntityId); student.Modify(request); await _repository.UpdateAsync(student, cancellationToken); }
public async Task <Guid> HandleAsync(CreateStudentRequest request, CancellationToken cancellationToken) { _logger.Debug("Creating student..."); var studentType = await _studentTypeRepository.GetByIdAsync(request.StudentTypeId, cancellationToken : cancellationToken); var student = await _studentRepository.AddAsync(new Student(request, studentType.Description), cancellationToken : cancellationToken); return(student.Id); }
public async Task HandleAsync(UpdateStudentRequest request, CancellationToken cancellationToken) { _logger.Debug("Updating student..."); var studentType = await _studentTypeRepository.GetByIdAsync(request.StudentTypeId, cancellationToken : cancellationToken); var student = await _studentRepository.GetByIdAsync(request.StudentId, cancellationToken : cancellationToken); student.Update(request, studentType.Description); await _studentRepository.UpdateAsync(student, cancellationToken : cancellationToken); }
public override async Task <TResponse> ExecuteAsync <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken, Func <TRequest, CancellationToken, Task <TResponse> > next) { _logger.Debug($"UnitOfWorkInterceptor started"); var unitOfWorkOptions = _uowOptionsBuilder.Build(request as IRequest <TResponse>); _logger.Debug($"About to start UoW with IsolationLevel: {unitOfWorkOptions.IsolationLevel.ToString()}"); TResponse response; using (_unitOfWork = _unitOfWorkManager.Get(unitOfWorkOptions)) { response = await next.Invoke(request, cancellationToken); _unitOfWork.Commit(); } _unitOfWork = null; _logger.Debug($"UnitOfWorkInterceptor ended"); return(response); }
public async Task HandleAsync(AddStudentRequest request, CancellationToken cancellationToken) { _logger.Debug($"{nameof(AddStudentHandler)} invoked"); var student = new Student(request.Name, request.Id); await _repository.AddAsync(student, cancellationToken); if (request.RaiseAnotherCreateEvent) { var student2 = new Student(request.Name + "2nd", CqrsConstants.Event3Id); await _repository.AddAsync(student2, cancellationToken); } }
private TResponse ExecuteInterceptors <TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> handle) { _logger.Debug("Running Interceptors..."); var next = _interceptors.Reverse().Aggregate(handle, (handleDelegate, interceptor) => (req) => interceptor.Run <IRequest <TResponse>, TResponse>(req, handleDelegate)); try { return(next.Invoke(request)); } catch (Exception ex) { _logger.Error(ex); throw; } finally { _interceptors.ToList().ForEach(m => m.Dispose()); } }
private async Task <TResponse> ExecuteInterceptorsAsync <TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, CancellationToken, Task <TResponse> > handleAsync, CancellationToken cancellationToken) { _logger.Debug("Running Interceptors..."); var next = _interceptors.Reverse().Aggregate(handleAsync, (handleDelegate, interceptor) => (req, token) => interceptor.RunAsync(req, token, handleDelegate)); try { return(await next.Invoke(request, cancellationToken).ConfigureAwait(false)); } catch (Exception ex) { _logger.Error(ex); throw; } finally { _interceptors.ToList().ForEach(m => m.Dispose()); } }
public async Task PurgeAsync(ICqrsEvent cqrsEvent, CancellationToken cancellationToken) { _logger.Debug($"Getting entity repository. TypeName: {cqrsEvent.SourceTypeName}"); var sourceEntityType = Type.GetType(cqrsEvent.SourceTypeName); var getRepositoryMethod = _cqrsRepositoryFactory.GetType().GetMethod("GetRepository"); if (getRepositoryMethod != null) { var genericMethodForSourceEntity = getRepositoryMethod.MakeGenericMethod(sourceEntityType); dynamic sourceEntityRepository = genericMethodForSourceEntity.Invoke(_cqrsRepositoryFactory, null); _logger.Debug($"Built {nameof(CqrsRepositoryFactory)}"); _logger.Debug($"Fetching entity by Id. Id: {cqrsEvent.SourceId}"); using var uow = _unitOfWorkManager.Get(); var cqrsEntity = await sourceEntityRepository.GetByIdAsync(cqrsEvent.SourceId, cancellationToken); var baseCqrsEntity = cqrsEntity as BaseCqrsEntity; _logger.Debug($"Fetched entity. Id: {cqrsEvent.SourceId}"); var @event = baseCqrsEntity?.EventsToBeProcessed.FirstOrDefault(f => f.Id == cqrsEvent.Id); if (@event != null) { _logger.Debug($"Removing event. Id: {@event.Id}"); baseCqrsEntity.RemoveEventToBeProcessed(@event); await sourceEntityRepository.UpdateAsync(cqrsEntity, cancellationToken); _logger.Debug("Removed event"); } uow.Commit(); } }
public IUnitOfWork Get(UnitOfWorkOptions unitOfWorkOptions = null) { if (unitOfWorkOptions == null) { unitOfWorkOptions = new UnitOfWorkOptions(); } _logger.Debug($"About to start UoW. IsolationLevel: {unitOfWorkOptions.IsolationLevel} " + $"TransactionTimeOut: {unitOfWorkOptions.TransactionTimeout}" + $"TransactionScopeOption: {unitOfWorkOptions.TransactionScopeOption}"); var unitOfWork = _unitOfWorkFactory.Create(unitOfWorkOptions); return(unitOfWork); }
public IUnitOfWork Get(UnitOfWorkOptions unitOfWorkOptions) { if (_isUoWInstantiated) { unitOfWorkOptions.TransactionScopeOption = System.Transactions.TransactionScopeOption.RequiresNew; } _logger.Debug($"About to start UoW. IsolationLevel: {unitOfWorkOptions.IsolationLevel} " + $"TransactionTimeOut: {unitOfWorkOptions.TransactionTimeout}" + $"TransactionScopeOption: {unitOfWorkOptions.TransactionScopeOption}"); var unitOfWork = _unitOfWorkFactory.Create(unitOfWorkOptions); _isUoWInstantiated = true; return(unitOfWork); }
private void Start() { _logger.Debug($"Starting UoW..."); _transactionScope = new TransactionScope(_unitOfWorkOptions.TransactionScopeOption, new TransactionOptions { IsolationLevel = _unitOfWorkOptions.IsolationLevel, Timeout = _unitOfWorkOptions.TransactionTimeout }, TransactionScopeAsyncFlowOption.Enabled); _logger.Debug("Started UoW"); }
public override TResponse Execute <TRequest, TResponse>(IRequest <TResponse> request, Func <IRequest <TResponse>, TResponse> next) { _logger.Debug($"UnitOfWorkInterceptor started"); var unitOfWorkOptions = _uowOptionsBuilder.Build(request); _logger.Debug($"About to start UoW with IsolationLevel: {unitOfWorkOptions.IsolationLevel.ToString()}"); TResponse response; using (_unitOfWork = _unitOfWorkManager.Get(unitOfWorkOptions)) { response = next.Invoke(request); _unitOfWork.Commit(); } _unitOfWork = null; _logger.Debug($"UnitOfWorkInterceptor ended"); return(response); }
public string GetName() { _logger.Debug("getting name..."); return("John"); }
public IUnitOfWork Create(UnitOfWorkOptions options) { _uowFactory.Debug("Creating new UoW"); return(new UnitOfWork(_boltOnLoggerFactory, options)); }
public async Task HandleAsync(StudentCreatedEvent request, CancellationToken cancellationToken) { _logger.Debug($"{nameof(StudentCreatedEventHandler)} invoked"); var testCqrsReadEntity = new StudentFlattened(request); await _repository.AddAsync(testCqrsReadEntity, cancellationToken); }