public async Task Create([FromBody] CreateItemCommand command)
        {
            // Start the "Outbox" transaction
            await _messaging.EnlistInTransaction(_db);

            // Create a new Item entity
            var item = new Item
            {
                Name = command.Name
            };

            // Add the item to the current
            // DbContext unit of work
            _db.Items.Add(item);

            // Publish an event to anyone
            // who cares that a new Item has
            // been created
            var @event = new ItemCreated
            {
                Id = item.Id
            };

            // Because the message context is enlisted in an
            // "outbox" transaction, these outgoing messages are
            // held until the ongoing transaction completes
            await _messaging.Send(@event);

            // Commit the unit of work. This will persist
            // both the Item entity we created above, and
            // also a Jasper Envelope for the outgoing
            // ItemCreated message
            await _db.SaveChangesAsync();

            // After the DbContext transaction succeeds, kick out
            // the persisted messages in the context "outbox"
            await _messaging.SendAllQueuedOutgoingMessages();
        }
Beispiel #2
0
 public void Handle(ItemCreated @event)
 {
     Console.WriteLine("You created a new item with id " + @event.Id);
 }