1
1
from seedwork .application .event_dispatcher import EventDispatcher
2
+ from seedwork .application .exceptions import UnitOfWorkNotSetException
2
3
from seedwork .infrastructure .logging import logger
3
4
4
5
@@ -110,18 +111,19 @@ def unit_of_work(self, **kwargs):
110
111
111
112
def create_unit_of_work (self , correlation_id , db_session , kwargs ):
112
113
"""Unit of Work factory, creates new unit of work"""
113
- uow = self . unit_of_work_class (
114
+ uow_kwargs = dict (
114
115
module = self ,
115
116
correlation_id = correlation_id ,
116
117
db_session = db_session ,
117
118
** self .get_unit_of_work_init_kwargs (),
118
119
** kwargs ,
119
120
)
121
+ uow = self .unit_of_work_class (** uow_kwargs )
120
122
return uow
121
123
122
124
def get_unit_of_work_init_kwargs (self ):
123
125
"""Returns additional kwargs used for initialization of new Unit of Work"""
124
- return self . init_kwargs
126
+ return {}
125
127
126
128
def configure_unit_of_work (self , uow ):
127
129
"""Allows to alter Unit of Work (i.e. add extra attributes) after it is instantiated"""
@@ -158,7 +160,10 @@ def execute_query(self, query):
158
160
def uow (self ) -> UnitOfWork :
159
161
"""Get current unit of work. Use self.unit_of_work() to create a new instance of UoW"""
160
162
uow = self ._uow .get ()
161
- assert uow , f"Unit of work not set in { self } , use context manager"
163
+ if uow is None :
164
+ raise UnitOfWorkNotSetException (
165
+ f"Unit of work not set in { self } , use context manager"
166
+ )
162
167
return uow
163
168
164
169
def resolve_handler_kwargs (self , kwarg_params ) -> dict :
@@ -172,29 +177,14 @@ def resolve_handler_kwargs(self, kwarg_params) -> dict:
172
177
173
178
def publish_domain_events (self , events ):
174
179
for event in events :
175
- self ._domain_event_dispatcher .dispatch (event = event , sender = self )
180
+ self ._domain_event_dispatcher .dispatch (event = event )
176
181
177
- def handle_domain_event (
178
- self , event : type [DomainEvent ], sender : type ["BusinessModule" ]
179
- ):
182
+ def handle_domain_event (self , event : type [DomainEvent ]):
180
183
"""Execute all registered handlers within this module for this event type"""
181
- event_was_sent_by_other_module = self is not sender
182
-
183
- if event_was_sent_by_other_module :
184
- # The sender executed the command that resulted in an event being published.
185
- # as a rule of thumb we want to handle the domain event within same transaction,
186
- # thus within same Unit of Work.
187
- # Therefore, the receiver must use UoW of sender
188
- original_uow = self ._uow .get ()
189
- self ._uow .set (sender .uow )
190
184
191
185
for handler in self .event_handlers :
192
186
event_class , handler_parameters = self .registry .inspect_handler_parameters (
193
187
handler
194
188
)
195
189
if event_class is type (event ):
196
190
handler (event , self )
197
-
198
- if event_was_sent_by_other_module :
199
- # restore the original UoW
200
- self ._uow .set (original_uow )
0 commit comments