mercredi 18 octobre 2017

Application Architecture re: Dependency Injection

I'm working in an application where we implement the Inversion of Control design pattern through Dependency Injection.

We are building a service, for example, that provides the ability to keep track of user changes (similar to a changelog). One of my colleagues has argued that if we want to track these changes, we should do so by creating a handler for this changelog in the services that provide this functionality.

For example, if we have the following service:

public Foo FooService(Bar bar){
      //do some crud operation
      //write to the changelog -> any handlers are here for Foo (the new object) to retrieve any dependencies required to write to the changelog service
}

They have suggested that the service be written as follows:

public Foo FooService(Bar bar){
      //do some crud operation
      //begin generating the diff required for the changelog
      //update the diff with an object update handler
      //write to the changelog
}

The handlers update Foo as necessary to write any pertinent information for FK constraints to the changelog.

The changelog service's current only responsibility is to write new records to the changelog.

Our primary point of contention is this: should the handlers go inside of the service for the changelog, or inside the service that contacted it?

I've suggested to them that the proper approach is to encapsulate the changelog's functionality separate from the other services. As such, the changelog would also have to have dependencies injected from services that may or may not be required from the service calling it; however, the goal is to encapsulate the act of writing to the changelog completely separate from the logic required to work with the object being written to the changelog.

They've suggested that this is not ideal, and that we should generate all of the information we need in the service that will write to the changelog.

Is my approach correct or is their approach correct? Why is what I'm suggesting good or bad?

Aucun commentaire:

Enregistrer un commentaire