#Review my simple message bus pattern implementation

2 messages · Page 1 of 1 (latest)

spice wing
#

Hello!

Since I'm new to Rust, I'd like to improve myself by submitting you a code review 🙂

core.rs

Contains the command bus implementation with its logic

https://play.rust-lang.org/?version=stable&edition=2021&gist=1257a71deec3c5e8dfc6bf615174aaa6

middleware.rs

Contains a list of available queued/"chainable" middlewares

https://play.rust-lang.org/?version=stable&edition=2021&gist=88b67db39effd6370e1a2e9a9daadca3

integration test

https://play.rust-lang.org/?version=stable&edition=2021&gist=cf13b43ba7348609b38ad2f1a7f6f833

Calling command_bus.dispatch(&AddItemToCart); shows:

Correlation id `in-memory`.
Received message `add_item_to_cart`.
Ping database.
Begin transaction.
Handling message `add_item_to_cart`.
Handled message `add_item_to_cart`.
Commit transaction.

Design

MessageHandlerMiddleware is responsible to call the associated message handler (not implemented yet but it's basically a lookup hash map between a command name and its referenced struct handler). That middleware may be the last one of the bus

I wanted that each middleware knows its next one for special use cases such as the TransactionMiddleware. Its responsibility is:

  • to begin a db transaction
  • to call the next middleware (the MessageHandlerMiddleware)
  • to commit the db changes if there was no error
  • to rollback the db changes if MessageHandlerMiddleware has returned a result error (TODO: return Result<..., Err> in Middleware::handle(...))

Feel free to give me any suggestion :)!

What I'd like to do next:

  • Make it possible for the bus to be thread shared (by using Arc if I understand)
  • Make it possible for the bus to async dispatch any message (I want it to be sync + async if possible or so have another implementation like SyncCommandBus and AsyncCommandBus)
#

Review my simple message bus pattern implementation