public void Handle(IHandlerContext context, FireAt command) { if (Data.TargetPosition == command.Position) { context.Publish(new Hit { Id = context.NewGuid(), GameId = command.GameId }); } else { context.Publish(new Missed { Id = context.NewGuid(), GameId = command.GameId }); } if (Data.NumberOfAttempts + 1 >= MaxAttemptsInARound) { Data.NumberOfAttempts = 0; Data.TargetPosition = context.Random.Next(0, 100); } else { Data.NumberOfAttempts++; } }
public void ProcessMessage(IHandlerContext <ArchiveOrderProcessCommand> context) { using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var orderProcess = _repository.Get(context.Message.OrderProcessId); if (!orderProcess.CanArchive()) { context.Publish(new ArchiveOrderProcessRejectedEvent { OrderProcessId = context.Message.OrderProcessId, Status = orderProcess.Status().Status }); return; } _repository.Remove(orderProcess); } context.Publish(new OrderProcessArchivedEvent { OrderProcessId = context.Message.OrderProcessId }); }
public void ProcessMessage(IHandlerContext <ArchiveOrderProcessCommand> context) { using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var stream = _eventStore.Get(context.Message.OrderProcessId); var orderProcess = new OrderProcess(context.Message.OrderProcessId); stream.Apply(orderProcess); if (!orderProcess.CanArchive) { context.Publish(new ArchiveOrderProcessRejectedEvent { OrderProcessId = context.Message.OrderProcessId, Status = orderProcess.Status }); return; } stream.AddEvent(orderProcess.ChangeStatus("Order Archived")); _eventStore.SaveEventStream(stream); } context.Publish(new OrderProcessArchivedEvent { OrderProcessId = context.Message.OrderProcessId }); }
public void ProcessMessage(IHandlerContext <CancelOrderProcessCommand> context) { using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var stream = _eventStore.Get(context.Message.OrderProcessId); var orderProcess = new OrderProcess(context.Message.OrderProcessId); stream.Apply(orderProcess); if (!orderProcess.CanCancel) { context.Publish(new CancelOrderProcessRejectedEvent { OrderProcessId = context.Message.OrderProcessId, Status = orderProcess.Status }); return; } _eventStore.Remove(context.Message.OrderProcessId); } context.Publish(new OrderProcessCancelledEvent { OrderProcessId = context.Message.OrderProcessId }); }
public void ProcessMessage(IHandlerContext <CreateOrderCommand> context) { // simulate slow processing Thread.Sleep(1000); var message = context.Message; var order = new Order(message.OrderNumber, message.OrderDate) { Customer = new OrderCustomer(message.CustomerName, message.CustomerEMail) }; foreach (var item in message.Items) { order.AddItem(item.Description, item.Price); } using (_databaseContextFactory.Create(OrderingData.ConnectionStringName)) { _repository.Add(order); } var orderCreatedEvent = new OrderCreatedEvent { OrderId = order.Id, OrderNumber = message.OrderNumber, OrderDate = message.OrderDate, CustomerName = message.CustomerName, CustomerEMail = message.CustomerEMail }; orderCreatedEvent.Items.AddRange(message.Items); context.Publish(orderCreatedEvent); }
public void ProcessMessage(IHandlerContext <AcceptOrderProcessCommand> context) { OrderProcess orderProcess; using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var stream = _eventStore.Get(context.Message.OrderProcessId); if (stream.IsEmpty) { return; } orderProcess = new OrderProcess(context.Message.OrderProcessId); stream.Apply(orderProcess); stream.AddEvent(orderProcess.ChangeStatus("Order Accepted")); _eventStore.SaveEventStream(stream); } context.Send(orderProcess.CreateOrderCommand(), c => c.WithCorrelationId(orderProcess.Id.ToString())); context.Publish(new OrderProcessAcceptedEvent { OrderProcessId = orderProcess.Id }); }
public void ProcessMessage(IHandlerContext <RegisterMemberCommand> context) { Console.WriteLine(); Console.WriteLine("[MEMBER REGISTERED] : user name = '{0}'", context.Message.UserName); Console.WriteLine(); context.Publish(new MemberRegisteredEvent { UserName = context.Message.UserName }); }
public void ProcessMessage(IHandlerContext <SendEMailCommand> context) { // simulate sending an e-mail Thread.Sleep(2000); context.Publish(new EMailSentEvent { EMailId = context.Message.EMailId }); }
public void ProcessMessage(IHandlerContext <RegisterOrderProcessCommand> context) { var message = context.Message; var orderProcess = new OrderProcess { OrderId = null, InvoiceId = null, CustomerName = message.CustomerName, CustomerEMail = message.CustomerEMail, TargetSystem = message.TargetSystem, TargetSystemUri = message.TargetSystemUri }; orderProcess.GenerateOrderNumber(); foreach (var quotedProduct in message.QuotedProducts) { orderProcess.AddItem(new OrderProcessItem(quotedProduct.ProductId, quotedProduct.Description, quotedProduct.Price)); } var status = new OrderProcessStatus("Cooling Off"); orderProcess.AddStatus(status); using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { _repository.Add(orderProcess); } context.Publish(new OrderProcessRegisteredEvent { OrderProcessId = orderProcess.Id, QuotedProducts = message.QuotedProducts, CustomerName = message.CustomerName, CustomerEMail = message.CustomerEMail, OrderNumber = orderProcess.OrderNumber, OrderDate = orderProcess.DateRegistered, OrderTotal = orderProcess.Total(), Status = status.Status, StatusDate = status.StatusDate, TargetSystem = message.TargetSystem, TargetSystemUri = message.TargetSystemUri }); context.Send(new AcceptOrderProcessCommand { OrderProcessId = orderProcess.Id }, c => c.Defer(DateTime.Now.AddSeconds(10)).Local()); }
public void ProcessMessage(IHandlerContext <RegisterOrderProcessCommand> context) { var message = context.Message; var orderProcess = new OrderProcess(); var stream = _eventStore.CreateEventStream(orderProcess.Id); var initialized = orderProcess.Initialize(); stream.AddEvent(initialized); stream.AddEvent(orderProcess.AssignCustomer(message.CustomerName, message.CustomerEMail)); stream.AddEvent(orderProcess.AssignTargetSystem(message.TargetSystem, message.TargetSystemUri)); var status = orderProcess.ChangeStatus("Cooling Off"); stream.AddEvent(status); foreach (var quotedProduct in message.QuotedProducts) { stream.AddEvent(orderProcess.AddItem(quotedProduct.ProductId, quotedProduct.Description, quotedProduct.Price)); } using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { _eventStore.Save(stream); } context.Publish(new OrderProcessRegisteredEvent { OrderProcessId = orderProcess.Id, QuotedProducts = message.QuotedProducts, CustomerName = message.CustomerName, CustomerEMail = message.CustomerEMail, OrderNumber = initialized.OrderNumber, OrderDate = initialized.DateRegistered, OrderTotal = orderProcess.Total, Status = status.Status, StatusDate = status.StatusDate, TargetSystem = message.TargetSystem, TargetSystemUri = message.TargetSystemUri }); context.Send(new AcceptOrderProcessCommand { OrderProcessId = orderProcess.Id }, c => c.Defer(DateTime.Now.AddSeconds(10)).Local()); }
public void ProcessMessage(IHandlerContext <RegisterUserCommand> context) { var message = context.Message; var id = Guid.NewGuid(); Registered registered; using (_databaseContextFactory.Create()) { var key = User.Key(message.EMail); if (_keyStore.Contains(key)) { if (!message.EMail.Equals("shuttle", StringComparison.InvariantCultureIgnoreCase)) { context.Send(new SendEMailCommand { Body = string.Format("<p>Hello,</p><br/><p>Unfortunately e-mail '{0}' has been assigned before your user could be registered. Please register again.</p><br/><p>Regards</p>", message.EMail), Subject = "User registration failure", IsBodyHtml = true, To = message.EMail }); } return; } _keyStore.Add(id, key); var user = new User(id); var stream = new EventStream(id); registered = user.Register(message.EMail, message.PasswordHash, message.RegisteredBy); stream.AddEvent(registered); _eventStore.SaveEventStream(stream); } context.Publish(new UserRegisteredEvent { Id = id, EMail = message.EMail, RegisteredBy = message.RegisteredBy, DateRegistered = registered.DateRegistered }); }
public void ProcessMessage(IHandlerContext <AcceptOrderProcessCommand> context) { OrderProcess orderProcess; using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { orderProcess = _repository.Get(context.Message.OrderProcessId); if (orderProcess == null) { // Probably cancelled in the meantime. // In a production system you would probably check this against some audit mechanism. return; } var orderProcessStatus = new OrderProcessStatus("Order Accepted"); orderProcess.AddStatus(orderProcessStatus); _repository.AddStatus(orderProcessStatus, orderProcess.Id); } var createOrderCommand = new CreateOrderCommand { OrderNumber = orderProcess.OrderNumber, OrderDate = orderProcess.DateRegistered, CustomerName = orderProcess.CustomerName, CustomerEMail = orderProcess.CustomerEMail }; foreach (var orderProcessItem in orderProcess.OrderProcessItems) { createOrderCommand.Items.Add(new MessageOrderItem { Description = orderProcessItem.Description, Price = orderProcessItem.Price }); } context.Send(createOrderCommand, c => c.WithCorrelationId(orderProcess.Id.ToString())); context.Publish(new OrderProcessAcceptedEvent { OrderProcessId = orderProcess.Id }); }
public void ProcessMessage(IHandlerContext <CancelOrderProcessCommand> context) { using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var orderProcess = _repository.Get(context.Message.OrderProcessId); if (!orderProcess.CanCancel()) { return; } _repository.Remove(orderProcess); } context.Publish(new OrderProcessCancelledEvent { OrderProcessId = context.Message.OrderProcessId }); }
public void ProcessMessage(IHandlerContext <CompleteOrderProcessCommand> context) { OrderProcess orderProcess; using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { var stream = _eventStore.Get(context.Message.OrderProcessId); orderProcess = new OrderProcess(context.Message.OrderProcessId); stream.Apply(orderProcess); stream.AddEvent(orderProcess.ChangeStatus("Completed")); _eventStore.SaveEventStream(stream); } context.Publish(new OrderProcessCompletedEvent { OrderProcessId = orderProcess.Id }); }
public void ProcessMessage(IHandlerContext <CompleteOrderProcessCommand> context) { OrderProcess orderProcess; using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName)) { orderProcess = _repository.Get(context.Message.OrderProcessId); var orderProcessStatus = new OrderProcessStatus("Completed"); orderProcess.AddStatus(orderProcessStatus); _repository.AddStatus(orderProcessStatus, orderProcess.Id); } context.Publish(new OrderProcessCompletedEvent { OrderProcessId = orderProcess.Id }); }
public void ProcessMessage(IHandlerContext<CreateStudentData> context) { var data = context.Message; // In a more "real" ESB, we would use this layer to perform more sophisticated // translations between a received event and an event published to the larger // system. For the sake of brevity, I have not "converted" all the fields received // from the event. context.Publish( new NewStudentData() { StudentId = data.StudentId, Name = data.Name, HomePhone = data.HomePhone, HomeAddress = data.HomeAddress, LocalAddress = data.LocalAddress, EmailAddress = data.EmailAddress, EmergencyContact = data.EmergencyContact, AcademicStatus = data.AcademicStatus, // ... } ); }
public void ProcessMessage(IHandlerContext <CreateInvoiceCommand> context) { // simulate slow processing Thread.Sleep(1000); var message = context.Message; var invoice = new Invoice(message.OrderId) { AccountContact = new InvoiceAccountContact(message.AccountContactName, message.AccountContactEMail) }; foreach (var item in message.Items) { invoice.AddItem(item.Description, item.Price); } invoice.GenerateInvoiceNumber(); using (_databaseContextFactory.Create(InvoicingData.ConnectionStringName)) { _repository.Add(invoice); } var orderCreatedEvent = new InvoiceCreatedEvent { InvoiceId = invoice.Id, InvoiceNumber = invoice.InvoiceNumber, InvoiceDate = invoice.InvoiceDate, AccountContactName = message.AccountContactName, AccountContactEMail = message.AccountContactEMail }; orderCreatedEvent.Items.AddRange(message.Items); context.Publish(orderCreatedEvent); }
public void ProcessMessage(IHandlerContext <RegisterDocumentCommand> context) { Guard.AgainstNull(context, nameof(context)); using (_databaseContextFactory.Create()) { var document = _documentRepository.Get(context.Message.Id); var status = !SuspiciousExpression.IsMatch(document.FileName) ? _malwareService.Register(document) : ServiceStatus.Suspicious; switch (status) { case ServiceStatus.Cleared: { document.Cleared(); _documentRepository.Save(document); context.Publish(new DocumentProcessedEvent { Id = document.Id, ReferenceId = document.ReferenceId, SystemName = document.SystemName, Suspicious = false }); break; } case ServiceStatus.Suspicious: { document.Suspicious(); _documentRepository.Save(document); context.Publish(new DocumentProcessedEvent { Id = document.Id, ReferenceId = document.ReferenceId, SystemName = document.SystemName, Suspicious = true }); break; } case ServiceStatus.Registered: case ServiceStatus.Processing: { _documentRepository.Save(document); context.Send(new PollDocumentCommand { Id = document.Id }, c => c.Defer(DateTime.Now.AddSeconds(5)).Local()); break; } } } }
public void ProcessMessage(IHandlerContext <PollDocumentCommand> context) { Guard.AgainstNull(context, nameof(context)); object commandMessage = null; object eventMessage = null; var pollIntervalTimeSpan = TimeSpan.Zero; using (_databaseContextFactory.Create()) { var document = _documentRepository.Get(context.Message.Id); var status = _malwareService.Poll(document); switch (status) { case ServiceStatus.Cleared: { document.Cleared(); _documentRepository.Save(document); eventMessage = new DocumentProcessedEvent { Id = document.Id, ReferenceId = document.ReferenceId, SystemName = document.SystemName, Suspicious = false }; break; } case ServiceStatus.Suspicious: { document.Suspicious(); _documentRepository.Save(document); eventMessage = new DocumentProcessedEvent { Id = document.Id, ReferenceId = document.ReferenceId, SystemName = document.SystemName, Suspicious = true }; break; } } switch (status) { case ServiceStatus.Processing: case ServiceStatus.Registered: { _documentRepository.Save(document); commandMessage = new PollDocumentCommand { Id = document.Id }; pollIntervalTimeSpan = TimeSpan.FromSeconds(5); if (document.ContainsProperty("PollIntervalTimeSpan")) { TimeSpan.TryParse(document.GetPropertyValue("PollIntervalTimeSpan"), out pollIntervalTimeSpan); } break; } } } if (commandMessage != null) { context.Send(commandMessage, c => { if (pollIntervalTimeSpan == TimeSpan.Zero) { return; } c.Defer(DateTime.Now.Add(pollIntervalTimeSpan)).Local(); }); } if (eventMessage != null) { context.Publish(eventMessage); } }