I am in process of app refactoring. Simple CRUD logic before was covered with Generic Repo & Unit Of Work pattern before. Right now the app receives more requirements and they are gonna be changing, thus to make the app be able to expanded I'm trying to use Event Driven architecture in BL. Here are the questions I'm struggling with:
- how to share the db context? passing it through the args feels like bad idea, but injecting them through the interface will not be able to guarantee that there's only one context.
- Should all the secondary operations covered in one transaction?
- What is the proper way to handle errors here? For example, UpdateException.
// for example, recalculating inventory after creating order or logging changes after order transaction
public class SomethingHappenedEvent : IDomainEvent{}
public class SomethingHappenedEventHandler : IDomainHandler<SomethingHappenedEvent>
{
public void Handle(SomethingHappenedEvent args)
{
// run db stuff with Entity Framework
}
}
Client class:
public class SomeBussinessLogic
{
public void ProccessSomeLogic()
{
// main responsibility of method
DomainEvent.Raise(new SomethingHappenedEvent();
}
}