jeudi 25 février 2016

Violation of Law of Demeter in Interactors/Use Cases/Application Services

Lately I have been looking through a bunch of ideas for software architecture. What I see is that a lot of them converge into having Use Cases/Interactors (Clean Architecture) or Application Services (DDD) as an entry point to our applications.

I really like the idea, but something has been bothering me.

Both ways the user injects the repository, which you use to fetch the domain entities and perform an action on it. Like this:

class Interactor
  def initialize(repository)
    @repository = repository
  end

  def call(entity_id)
    entity = @repository.find(entity_id)
    entity.do_something
  end
end

If I want to test this in isolation, I need to return a mock from @repository.find, which already is a mock. That is not good and tells me that I am violating the Law of Demeter (which is the case).

Also, this is too procedural, which bothers me.

Is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire