예제 #1
0
        public static ItemCreated Handle(
            // This would be the message
            CreateItemCommand command,

            // Any other arguments are assumed
            // to be service dependencies
            ItemsDbContext 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);

            // This event being returned
            // by the handler will be automatically sent
            // out as a "cascading" message
            return(new ItemCreated
            {
                Id = item.Id
            });
        }
        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();
        }
 public Task <ItemCreated> Create([FromBody] CreateItemCommand command)
 {
     // Using Jasper as a Mediator, and receive the
     // expected response from Jasper
     return(_bus.Invoke <ItemCreated>(command));
 }
 public Task Create([FromBody] CreateItemCommand command)
 {
     // Using Jasper as a Mediator
     return(_bus.Invoke(command));
 }