1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
//! [event-driven-core]: https://docs.rs/event-driven-core
//! [event-driven-macro]: https://docs.rs/event-driven-macro
//! [Command]: https://docs.rs/event-driven-core/latest/event_driven_core/message/trait.Command.html
//! [Event]: https://docs.rs/event-driven-core/latest/event_driven_core/message/trait.Message.html
//! [MessageBus]: https://docs.rs/event-driven-core/latest/event_driven_core/messagebus/index.html
//! [Context]: https://docs.rs/event-driven-core/latest/event_driven_core/messagebus/struct.ContextManager.html
//! [AtomicContextManager]: https://docs.rs/event-driven-core/latest/event_driven_core/messagebus/type.AtomicContextManager.html
//!
//! A event-driven framework for writing reliable and scalable system.
//!
//! At a high level, it provides a few major components:
//!
//! * Tools for [core components with traits][event-driven-core],
//! * [Macros][event-driven-macro] for processing events and commands
//!
//!
//! # A Tour of Event-Driven-Library
//!
//! Event-Driven-Library consists of a number of modules that provide a range of functionality
//! essential for implementing messagebus-like applications in Rust. In this
//! section, we will take a brief tour, summarizing the major APIs and
//! their uses.
//!
//! ## Command & Event
//! You can register any general struct with [Command] Derive Macro as follows:
//! ```ignore
//! #[derive(Command)]
//! pub struct MakeOrder {
//! pub user_id: i64,
//! pub items: Vec<String>,
//! }
//! ```
//! As you attach [Command] derive macro, MessageBus now is going to be able to understand how and where it should
//! dispatch the command to.
//!
//! Likewise, you can do the same thing for Event:
//! ```ignore
//! #[derive(Serialize, Deserialize, Clone, Message)]
//! #[internally_notifiable]
//! pub struct OrderFailed {
//! #[identifier]
//! pub user_id: i64,
//! }
//!
//! #[derive(Serialize, Deserialize, Clone, Message)]
//! #[internally_notifiable]
//! pub struct OrderSucceeded{
//! #[identifier]
//! pub user_id: i64,
//! pub items: Vec<String>
//! }
//! ```
//! Note that use of `internally_notifiable`(or `externally_notifiable`) and `identifier` is MUST.
//!
//! * `internally_notifiable` is marker to let the system know that the event should be handled
//! within the application
//! * `externally_notifiable` is to leave `OutBox`.
//! * `identifier` is to record aggregate id.
//!
//!
//!
//! ## Initializing Command Handlers
//! Command handlers are responsible for handling commands in an application, the response of which is sent directly to
//! clients. Commands are imperative in nature, meaning they specify what should be done.
//!
//! ```
//! # const IGNORE_1: &str = stringify! {
//! use event_driven_library::prelude::{init_command_handler, init_event_handler};
//! # };
//! # macro_rules! init_command_handler {
//! # ($($tt:tt)*) => {}
//! # }
//!
//! init_command_handler!(
//! {
//! MakeOrder: OrderHandler::make_order,
//! CancelOrder: OrderHandler::cancel_order
//! }
//! );
//! ```
//! In the example above, you see `MakeOrder` is mapped to `OrderHandler::make_order`, handler in application layer.
//!
//! At this point, imagine you want to handle both success/failure case of the `MakeOrder` command processing.
//! Then you have to think about using event handlers.
//!
//! ## Registering Event
//!
//! `Event` is a side effect of [Command] or yet another [Event] processing.
//! You can register as many handlers as possible as long as they all consume same type of Event as follows:
//!
//! ### Example
//!
//! ```
//! # macro_rules! init_event_handler {
//! # ($($tt:tt)*) => {}
//! # }
//! init_event_handler!(
//! {
//! OrderFaild: [
//! NotificationHandler::send_mail,
//! ],
//! OrderSucceeded: [
//! DeliveryHandler::checkout_delivery_items,
//! InventoryHandler::change_inventory_count
//! ]
//! }
//! );
//! ```
//! In the `MakeOrder` Command Handling, we have either `OrderFailed` or `OrderSucceeded` event with their own processing handlers.
//! Events are raised in the handlers that are thrown to [MessageBus] by [Context].
//! [MessageBus] then loops through the handlers UNLESS `StopSentinel` is received.
//!
//! ## Handler API Example
//!
//! Handlers can be located anywhere as long as they accept two argument:
//!
//! * msg - either [Command] or [Event]
//! * context - [AtomicContextManager]
//!
//! ### Example
//! ```ignore
//! pub async fn make_order(
//! cmd: MakeOrder,
//! context: AtomicContextManager,
//! ) -> Result<ServiceResponse, ServiceError> {
//! let mut uow = UnitOfWork::<Repository<OrderAggregate>, TExecutor>::new(context).await;
//!
//! let mut order_aggregate = OrderAggregate::new(cmd);
//! uow.repository().add(&mut task_aggregate).await?;
//!
//! uow.commit::<ServiceOutBox>().await?;
//!
//! Ok(().into())
//! }
//! ```
//! But sometimes, you may want to add yet another dependencies. For that, Dependency Injection mechanism has been implemented.
//! So, you can also do something along the lines of:
//! ```ignore
//! pub async fn make_order(
//! cmd: MakeOrder,
//! context: AtomicContextManager,
//! payment_gateway_caller: Box<dyn Fn(String, Value) -> Future<(), ServiceError> + Send + Sync + 'static> //injected dependency
//! ) -> Result<ServiceResponse, ServiceError> {
//! let mut uow = UnitOfWork::<Repository<OrderAggregate>, TExecutor>::new(context).await;
//!
//! let mut order_aggregate = OrderAggregate::new(cmd,payment_gateway_caller);
//! uow.repository().add(&mut task_aggregate).await?;
//!
//! uow.commit::<ServiceOutBox>().await?;
//!
//! Ok(().into())
//! }
//! ```
//!
//! How is this possible? because we preprocess handlers so it can allow for `DI container`.
//!
//! ## Dependency Injection
//! You can simply register dependencies by putting attribute on top of free function.
//!
//! ### Example
//!
//! ```ignore
//! // crate::dependencies
//! pub fn payment_gateway_caller() -> Box<dyn Fn(String, Value) -> Future<(), ServiceError> + Send + Sync + 'static> {
//! if cfg!(test) {
//! __test_payment_gateway_caller() //Dependency For Test
//! } else {
//! __actual_payment_gateway_caller() //Real Dependency
//! }
//! }
//! ```
//!
//! This is great as you can take your mind off static nature of the language.
//!
//!
//!
//! ## MessageBus
//! At the core is event driven library is [MessageBus], which gets command and gets raised event from
//! `UnitOfWork` and dispatch the event to the right handlers.
//! As this is done only in framework side, the only way you can 'feel' the presence of messagebus is
//! when you invoke it. Everything else is done magically.
//!
//!
//!
//! ### Example
//! ```ignore
//! #[derive(Command)]
//! pub struct MakeOrder { // Test Command
//! pub user_id: i64,
//! pub items: Vec<String>
//! }
//!
//! async fn test_func(){
//! let bus = MessageBus::new(command_handler(), event_handler())
//! let command = MakeOrder{user_id:1, items:vec!["shirts","jeans"]}
//! match bus.handle(command).await{
//! Err(err)=> { // test for error case }
//! Ok(val)=> { // test for happy case }
//! }
//! }
//! }
//! }
//! ```
//!
//! #### Error from MessageBus
//! When command has not yet been regitered, it returns an error - `BaseError::NotFound`
//! Be mindful that bus does NOT return the result of event processing as in distributed event processing.
pub extern crate event_driven_core;
pub extern crate event_driven_macro;
pub extern crate static_assertions;
pub mod prelude {
pub use event_driven_core::event_macros::*;
pub use event_driven_core::message::{Aggregate, Command, Message, MessageMetadata};
pub use event_driven_core::prelude::*;
pub use event_driven_macro::{entity, message_handler, Aggregate, ApplicationError, Command, Message};
}
#[cfg(test)]
mod application_error_derive_test {
use std::fmt::Display;
use crate as event_driven_library;
use event_driven_core::message::Message;
use event_driven_core::responses::{AnyError, BaseError};
use event_driven_macro::ApplicationError;
#[derive(Debug, ApplicationError)]
#[crates(event_driven_library)]
enum Err {
#[stop_sentinel]
Items,
#[stop_sentinel_with_event]
StopSentinelWithEvent(Box<dyn Message>),
#[database_error]
DatabaseError(Box<AnyError>),
BaseError(BaseError),
}
impl Display for Err {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Items => write!(f, "items"),
Self::StopSentinelWithEvent(item) => write!(f, "{:?}", item),
Self::DatabaseError(err) => write!(f, "{:?}", err),
Self::BaseError(err) => write!(f, "{:?}", err),
}
}
}
}