Disintegrate
Disintegrate is a Rust library that provides an alternative approach to building domain objects from an event stream. While supporting traditional aggregates, Disintegrate introduces a novel method that allows for more flexibility and adaptability in modeling business rules.
Why Disintegrate?
Disintegrate offers a fresh perspective on designing business applications by shifting away from relying solely on aggregates. Instead, it enables developers to construct business concepts directly from an event stream. This approach allows for decentralized and dynamic architectures that can evolve over time.
By leveraging the event stream as the foundation, Disintegrate empowers developers to build models that capture the essence of business events without the need for multiple versions of the same event within aggregates. This reduces duplication and complexity, leading to cleaner and more maintainable code.
Key Features
-
Event Stream Modeling: Disintegrate enables the construction of business concepts directly from an event stream, providing a more flexible and decentralized approach.
-
Support for Aggregates: While promoting a new approach, Disintegrate still supports traditional aggregates, allowing developers to transition gradually or utilize both methodologies in their applications.
-
Adaptability to Changing Business Rules: Disintegrate allows for easier evolution and adaptation to evolving business rules over time. By decoupling models from aggregates, developers gain the freedom to adjust and refine business concepts without heavy dependencies.
Usage
To add Disintegrate to your project, follow these steps:
-
Add
disintegrate
anddisintegrate-postgres
as a dependencies in yourCargo.toml
file:[] = "0.2.0" = "0.2.0"
- Disintegrate provides several features that you can enable based on your project requirements. You can include them in your
Cargo.toml
file as follows:
[] = { = "0.2.0", = ["macros", "serde-prost"] } = { = "0.2.0", = ["listener"] }
-
The macros feature enables the use of derive macros to simplify events implementations.
-
For events serialization and deserialization, Disintegrate supports different serialization formats through the Serde ecosystem. You can enable the desired format by including the corresponding feature:
- To enable JSON serialization, use the
serde-json
feature:features = ["serde-json"]
. - To enable Avro serialization, use the
serde-avro
feature:features = ["serde-avro"]
. - To enable Prost serialization, use the
serde-prost
feature:features = ["serde-prost"]
. - To enable Protocol Buffers serialization, use the
serde-protobuf
feature:features = ["serde-protobuf"]
.
- To enable JSON serialization, use the
-
If you're using the PostgreSQL event store backend and want to use the listener mechanism, you can enable the
listener
feature:disintegrate-postgres = {version = "0.2.0", features = ["-listener"]
}.
- Disintegrate provides several features that you can enable based on your project requirements. You can include them in your
-
Define the list of events in your application. You can use the Event Storming technique to identify the events that occur in your system. Here's an example of defining events using Disintegrate:
use Event; use ;
In this example, we define an enum
DomainEvent
using the#[derive(Event)]
attribute. The enum represents various events that can occur in your application. The#[group]
attribute specifies the event groups, such asUserEvent
andCartEvent
, and their corresponding variants. This allows you to organize events into logical groups. The#[id]
attribute on fields allows you to specify the domain identifiers of each event, which are used for filtering relevant events for a state. -
Create a state that implements the
State
trait. A state represents a business concept in your application and contains the information necessary to make decisions based on a group of events. Here's an example of a state:use crate CartEvent; use ; use HashSet; use Error; /// Implement your business logic using the state
In this example, we define a
Cart
struct that implements theState
trait. TheCart
struct represents the state of a shopping cart and keeps track of the items added by users. -
Instantiate an event store, hydrate the
Cart
state, and invokeadd_item
method:use Cart; use DomainEvent; use ; use ; use PgEventStore; use ; async
For a complete example, take a look at examples
folder to get a better understanding of how to use Disintegrate in a real-world application.
License
This project is licensed under the MIT License.
Contribution
Contributions are welcome! If you find any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request.
Please make sure to follow the Contributing Guidelines when making contributions to this project.
We appreciate your help in making Disintegrate better!
Acknowledgments
Disintegrate is inspired by the ideas presented in the talk Kill Aggregate! by Sara Pellegrini, exploring new possibilities for modeling business concepts from event streams. We would like to express our gratitude to the speaker for sharing their insights and sparking innovative thinking within the software development community.