Limit your abstractions: Application Events–what about change?
Join the DZone community and get the full member experience.
Join For Freein my previous post, i showed an example of application events and asked what is wrong with them.
public class cargoinspectionserviceimpl : icargoinspectionservice { // code redacted for simplicity public override void inspectcargo(trackingid trackingid) { validate.notnull(trackingid, "tracking id is required"); cargo cargo = cargorepository.find(trackingid); if (cargo == null) { logger.warn("can't inspect non-existing cargo " + trackingid); return; } handlinghistory handlinghistory = handlingeventrepository.lookuphandlinghistoryofcargo(trackingid); cargo.derivedeliveryprogress(handlinghistory); if (cargo.delivery.misdirected) { applicationevents.cargowasmisdirected(cargo); } if (cargo.delivery.unloadedatdestination) { applicationevents.cargohasarrived(cargo); } cargorepository.store(cargo); } }
this is very problematic code, from my point of view, for several reasons. look at how it allocate responsibilities. iapplicationevents is supposed to execute the actual event, but deciding when to execute the event is left for the caller. i said several reasons, but this is the main one, all other flows from it.
what happen when the rules for invoking an event change? what happen when we want to add a new event?
the way this is handled is broken. it violates the open closed principle, it violates the single responsibility principle and it frankly annoys me.
can you think about ways to improve this?
i’ll discuss some in my next post.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments