Skip to content

Commit

Permalink
Merge pull request #16 from mr-raccoon-97/main
Browse files Browse the repository at this point in the history
Add default events
  • Loading branch information
mapache-software authored Nov 15, 2024
2 parents 97041d7 + f73bf10 commit cd86977
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
25 changes: 25 additions & 0 deletions pybondi/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from abc import ABC
from dataclasses import dataclass
from pybondi.aggregate import Aggregate
from pybondi.messagebus import Event

@dataclass
class Added[T: Aggregate](Event):
'''
The Added[Aggregate] event is used to signal that the aggregate has been added to a session.
'''
aggregate: T

@dataclass
class RolledBack[T: Aggregate](Event):
'''
The RolledBack[Aggregate] event is used to signal that the aggregate has been rolled back in the session.
'''
aggregate: T

@dataclass
class Saved[T: Aggregate](Event):
'''
The Saved[Aggregate] event is used to signal that the aggregate has been committed in the session.
'''
aggregate: T
9 changes: 5 additions & 4 deletions pybondi/messagebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

class Event(ABC):
"""
Event is an abstract base class for domain events.
An abstract base class for domain events. All events should inherit from this class, otherwise
they will not be recognized by the message bus.
"""

class Command(ABC):
"""
Command is a class representing a request to perform an action.
Command is a class representing a request to perform an action. All commands should inherit from this class.
Otherwise, they will not be recognized by the message bus.
"""
...
@abstractmethod
def execute(self):
"""
Executes the command.
"""
...
raise NotImplementedError

class Messagebus:
"""
Expand Down
22 changes: 18 additions & 4 deletions pybondi/session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from collections import deque
from typing import Callable

from dataclasses import dataclass
from pybondi.aggregate import Aggregate
from pybondi.messagebus import Messagebus, Command, Event
from pybondi.repository import Repository
from pybondi.publisher import Publisher
from pybondi.events import Added, RolledBack, Saved

class Session:
"""
Expand Down Expand Up @@ -73,9 +74,12 @@ def dequeue(self) -> Command | Event:

def add(self, aggregate: Aggregate):
"""
Adds an aggregate to the repository.
Adds an aggregate to the repository. Enqueues an Added event for the aggregate.
In case that the aggregate state depends on external resources, the aggregate should
bring it's state up to date using a handler for the Added event.
"""
self.repository.add(aggregate)
self.enqueue(Added(aggregate))

def dispatch(self, message: Command | Event):
"""
Expand Down Expand Up @@ -115,14 +119,24 @@ def begin(self):

def commit(self):
"""
Commits changes from the transaction.
Commits changes from the transaction. Dispatches a Saved event for each aggregate
for which changes were committed. In case that the aggregate state depends on external
resources, it should be updated using a handler for the Saved event.
"""
self.run()
for aggregate in self.repository.aggregates.values():
self.dispatch(Saved(aggregate))
self.repository.commit(), self.publisher.commit()

def rollback(self):
"""
Rolls back changes of the transaction.
Rolls back changes of the transaction. Dispatches a RolledBack event for each aggregate
for which changes were rolled back. In case that the aggregate state depends on external
resources, it should be restored to its previous state using a handler for the RolledBack event.
"""
for aggregate in self.repository.aggregates.values():
self.dispatch(RolledBack(aggregate))
self.queue.clear()
self.repository.rollback(), self.publisher.rollback()

def close(self):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybondi"
version = "0.3.2"
version = "1.0.0"
description = "A lightweight library for creating event driven systems using domain driven design."
authors = ["Eric Cardozo <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit cd86977

Please sign in to comment.