Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Meetup Commands and Domain Events

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Avatar for Ron Darby Ron Darby
October 29, 2014

Laravel Meetup Commands and Domain Events

Presentation For Laravel Meetup - Cape Town

Avatar for Ron Darby

Ron Darby

October 29, 2014

Other Decks in Programming

Transcript

  1. THE COMMAND WHY?? Encourages S.O.L.I.D principles Which Allows for: •

    Easy Maintenance • Easy to Extend • Clean Testable Code
  2. COMMAND BUS Don’t ask, what is a Command Bus? It

    transports an Object from A to B • In Controller, Create Command ( Data Transfer Object ) • Pass the DTO into the Bus • The Bus is going to resolve the handler for that command using the IOC container • The CommandHandler will then delegate the DTO to a Model • The Model will then process the data and raise various Events • CommandHandler will then dispatch the Events and return Model to Controller The question should be, what does a Command Bus do?
  3. COMMAND BUS Inside Command Bus? class CommandBus { public function

    execute( $command ) { $handler = str_replace( 'Command', 'CommandHandler', get_class( $command ) ); if( !class_exists( $handler ) ) { $message = "CommandHandler $handler does not exist"; throw new Exception( $message ); } return App::make( $handler )->handle( $command ); } } interface CommandHandler { public function handle($command); } Command Handler Interface
  4. SOME CODE CONTROLLER class OrdersController extends BaseController { public function

    postPlace() { // Some Validation // Fetch cart items // Create an Order // Email Buyer and Seller // Redirect User for Payment } public function postItn() { // Some validation // Check if transaction duplicate, if not continue // Update the order status and create transaction // Email Buyer and Seller // Update Stock Levels } }