11from seedwork .application .event_dispatcher import EventDispatcher
2+ from seedwork .application .exceptions import UnitOfWorkNotSetException
23from seedwork .infrastructure .logging import logger
34
45
@@ -110,18 +111,19 @@ def unit_of_work(self, **kwargs):
110111
111112 def create_unit_of_work (self , correlation_id , db_session , kwargs ):
112113 """Unit of Work factory, creates new unit of work"""
113- uow = self . unit_of_work_class (
114+ uow_kwargs = dict (
114115 module = self ,
115116 correlation_id = correlation_id ,
116117 db_session = db_session ,
117118 ** self .get_unit_of_work_init_kwargs (),
118119 ** kwargs ,
119120 )
121+ uow = self .unit_of_work_class (** uow_kwargs )
120122 return uow
121123
122124 def get_unit_of_work_init_kwargs (self ):
123125 """Returns additional kwargs used for initialization of new Unit of Work"""
124- return self . init_kwargs
126+ return {}
125127
126128 def configure_unit_of_work (self , uow ):
127129 """Allows to alter Unit of Work (i.e. add extra attributes) after it is instantiated"""
@@ -158,7 +160,10 @@ def execute_query(self, query):
158160 def uow (self ) -> UnitOfWork :
159161 """Get current unit of work. Use self.unit_of_work() to create a new instance of UoW"""
160162 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+ )
162167 return uow
163168
164169 def resolve_handler_kwargs (self , kwarg_params ) -> dict :
@@ -172,29 +177,14 @@ def resolve_handler_kwargs(self, kwarg_params) -> dict:
172177
173178 def publish_domain_events (self , events ):
174179 for event in events :
175- self ._domain_event_dispatcher .dispatch (event = event , sender = self )
180+ self ._domain_event_dispatcher .dispatch (event = event )
176181
177- def handle_domain_event (
178- self , event : type [DomainEvent ], sender : type ["BusinessModule" ]
179- ):
182+ def handle_domain_event (self , event : type [DomainEvent ]):
180183 """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 )
190184
191185 for handler in self .event_handlers :
192186 event_class , handler_parameters = self .registry .inspect_handler_parameters (
193187 handler
194188 )
195189 if event_class is type (event ):
196190 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