public async Task Handle(AddTicketCommentCommand message)
        {
            var ticket = await _repository.Get <Ticket>(message.TicketId);

            ticket.AddComment(message.Comment, message.UserId);
            await _repository.Save(ticket);
        }
Example #2
0
        protected override async Task HandleCore(AddTicketCommentCommand message)
        {
            var ticket = await _repository.Get <Ticket>(message.TicketId);

            ticket.AddComment(message.Comment, message.UserId);
            await _repository.Save(ticket);
        }
Example #3
0
        protected override async Task HandleCore(UpdateTicketPriorityCommand message)
        {
            var ticket = await _repository.Get <Ticket>(message.TicketId);

            ticket.UpdatePriority(message.PriorityId, message.UserId);
            await _repository.Save(ticket);
        }
        protected override async Task HandleCore(UpdateTicketStatusCommand message)
        {
            var ticket = await _repository.Get <Ticket>(message.TicketId);

            ticket.UpdateStatus(message.StatusId, message.UserId);
            await _repository.Save(ticket);
        }
        public async Task <T> Get <T>(Guid aggregateId) where T : AggregateRoot
        {
            try
            {
                T aggregate;
                if (_cache.IsTracked(aggregateId))
                {
                    aggregate = (T)_cache.Get(aggregateId);
                    var events = (await _eventStore.Get <T>(aggregateId, aggregate.Version)).ToList();

                    // Check if there are any new events added between save in cache and now!
                    // If this is the case remove aggregate from cache and retrieve it.
                    if (events.Any() && events.First().Version != aggregate.Version)
                    {
                        _cache.Remove(aggregateId);
                    }
                    else
                    {
                        aggregate.LoadFromHistory(events);
                        return(aggregate);
                    }
                }

                aggregate = await _repository.Get <T>(aggregateId);

                _cache.Set(aggregateId, aggregate);
                return(aggregate);
            }
            catch (Exception)
            {
                _cache.Remove(aggregateId);
                throw;
            }
        }
Example #6
0
 public Aggregate Get(int id, [FromQuery] string title)
 {
     if (!string.IsNullOrEmpty(title))
     {
         _aggregateRepository.GetByTitle(title);
     }
     return(_aggregateRepository.Get(id));
 }
        public async Task Consume(ConsumeContext <CreateArticle> context)
        {
            var article = _aggregateRepository.Get <Domain.Article>(context.Message.Id);

            article.Create(context.Message);
            _aggregateRepository.Save(article);
            await Console.Out.WriteLineAsync($"Article Created : {context.Message.ToJson()}");
        }
        public async Task Should_persist_an_aggregate_with_100_events()
        {
            // Arrange
            var car = new Car(_carId);

            for (var i = 1; i <= 100; i++)
            {
                car.Travel(i);
            }

            // Act
            await _repository.Save(car);

            var savedModel = await _repository.Get(_carId);

            // Assert
            savedModel.Should().BeEquivalentTo(car);
        }
        public void Execute(TCommand command)
        {
            var agg = _aggRepository.Get(command.Id);

            (agg as IAggHandleCommand <TCommand>).Execute(command);
            var events = agg.GetUncommittedChanges();

            _aggRepository.Save(agg.Id, events, command.AggregateVersion);
            _eventPublisher.PublishEvents(events);
        }
 public async Task Handle(RetryMail command, IAggregateRepository repository)
 {
     var mail = new Mail();
     var load = await repository.Get();
     mail.LoadFromHistory(load.List);
     mail.RetryMail();
     await repository.Update(mail.RaiseNewEvents(), load.LastEventNumber);
     await mail.SendMail(this.mailService);
     await repository.Update(mail.RaiseNewEvents());
 }
Example #11
0
        public Handlers(IAggregateRepository repository)
        {
            Register <AdmitPatient>(async c =>
            {
                var encounter = new Encounter(c.PatientId, c.PatientName, c.AgeInYears, c.WardNumber);
                await repository.Save(encounter);
            });

            Register <TransferPatient>(async c =>
            {
                var encounter = await repository.Get <Encounter>(c.PatientId);
                encounter.Transfer(c.WardNumber);
                await repository.Save(encounter);
            });

            Register <DischargePatient>(async c =>
            {
                var encounter = await repository.Get <Encounter>(c.PatientId);
                encounter.DischargePatient();
                await repository.Save(encounter);
            });
        }
        public PaymentProcessorTests()
        {
            var today        = new DateTimeOffset(new DateTime(2019, 08, 19, 0, 0, 0, DateTimeKind.Utc));
            var dateProvider = Substitute.For <IProvideDateTime>();

            dateProvider.UtcNow().Returns(today);

            var payment = PaymentFactory.Create(dateProvider);

            _paymentId = payment.Id;

            _paymentService = Substitute.For <IPaymentService>();
            _bank           = Substitute.For <IAcquiringBank>();
            _paymentRepo    = Substitute.For <IAggregateRepository <Payment, PaymentId> >();
            _paymentRepo.Get(Arg.Is(_paymentId)).Returns(payment);
        }
        private async Task SaveAndTest(IAggregateRepository<IHomogeneousRole<RoleIdentifier>, RoleIdentifier> repo, params IHomogeneousRole<RoleIdentifier>[] roles)
        {
            foreach (var role in roles)
                await repo.Save(role);

            foreach (var role in roles)
            {
                var reconstituted = await repo.Get(role.Id);

                reconstituted.Should().NotBeNull();
                reconstituted.Should().NotBeSameAs(role);
                reconstituted.Id.Should().Be(role.Id);
                reconstituted.Version.Should().Be(role.Version);
                reconstituted.Name.Should().Be(role.Name);
                reconstituted.Status.Should().Be(role.Status);
            }
        }
        public async Task <Payment> ProcessPayment(PaymentId paymentId)
        {
            if (paymentId == null)
            {
                throw new ArgumentNullException(nameof(paymentId));
            }

            var payment = await _paymentRepository.Get(paymentId);

            if (_paymentService.HasAlreadyBeenProcessed(payment))
            {
                throw new PaymentAlreadyProcessedException(paymentId);
            }

            var transaction = await _bank.ProcessPayment(payment);

            _paymentService.UpdatePayment(payment, transaction);

            await _paymentRepository.Save(payment);

            return(payment);
        }
 public TAggregate GetByNaturalKey(TNaturalKey id)
 {
     return(_internalRepository.Get(_naturalKeyToAggregateIdMap[id]));
 }
Example #16
0
 public Task <T> Get <T>(string aggregateId, CancellationToken cancellationToken = default) where T : AggregateRoot
 => !_snapshotStrategy.IsSnapshottable(typeof(T))
         ? _decoratedRepository.Get <T>(aggregateId, cancellationToken)
 : LoadFromSnapshot(aggregateId, _aggregateFactory.CreateAggregate <T>(), cancellationToken);
Example #17
0
 public IAggregateRoot Get(string aggregateRootId)
 {
     return(_aggregateRepository.Get(aggregateRootId));
 }