Are observers really necessary if my service classes are responsible for all CRUD operations?

33 Views Asked by At

I'm building an API (PHP, Laravel) to serve data that I control to various clients that I also control.

My pattern is to coerce the incoming request into a DTO, then immediately push this to a dedicated service that matches the class of object (e.g. EventService). The services all have a set of base methods related to CRUD actions. (e.g. delete(), update(), create()) and then a bunch of additional ones that are specific to the type of record being manipulated (e.g. EventService->deleteAttendee())

The idea is that all db related operations go through each of these service classes.

My dilemma is whether to use observers or not. I have created some observers that watch for updates, creates etc. and do some extra work where needed. However, I'm starting to wonder whether this is just adding complexity for the sake of it. If I can be sure that everything goes through the correct service class method, would it be simpler to simply hook the additional operations into the service class methods? One big advantage of this I see is that developers will know where to find the bit of code that is doing the work - i.e. it will be somewhere in the correct service class.

It is also true that these observers would be calling service class methods anyway (as this is where I want to keep all of the important logic) which to my mind will further disorientate the future developer.

0

There are 0 best solutions below