4

Considering Uncle Bob's Clean Architecture (or Onion Architecture)

I have the following scenario:

  • I want to show the user, information of a place: name, category, coordinate.
  • I also want on the same screen, a button, that when clicked, redirects to a Map app showing the location of that place.

To redirect to the Map app, there is a simple framework API that receives a coordinate.

For the first problem, I should have a RetrievePlaceInformationInteractor (use case) that would receive a place id and return the name, category and coordinate.

But for the second problem, I don't know if I should:

  • have a separate OpenPlaceInMapInteractor (use case) that would receive the place id and return the coordinate to be used by that framework API in the Presenter.
  • have a separate OpenPlaceInMapInteractor (use case) that would receive the place id and use that framework API to redirect to the Map app.
  • use the same RetrievePlaceInformationInteractor to get the coordinate and fill it in a callback, that calls the framework API, and that would be called when the OpenInMap button was clicked.

The first seems a bit stupid, since it would essentially be an interactor just to retrieve one property, that is already being retrieved by another interactor.

The second forces the OpenPlaceInMapInteractor to access a framework API, which kinda defeats the purpose of the interactor layer not accessing the above layers (sure, I could use a protocol for the API and use dependency injection, but still, I'm relying on a framework specific feature).

The third seems reasonable, but wouldn't I be implementing 2 use cases in one?

What should I do?

Thank you.

Jesse
  • 3,243
  • 1
  • 22
  • 29
Rodrigo Ruiz
  • 4,248
  • 6
  • 43
  • 75

1 Answers1

1

Implement a RetrievePlaceInformationInteractor and a OpenPlaceInMapInteractor in the use-cases layers, that both internally call getPlaceDetails from the data access layer, which is where you want to do inversion of dependencies, so that the DB layer depends on the use-cases.

This way you do not repeat yourself, you assign proper names to each of the two use cases. You keep rightfully them separate from one-another, because they are not identical, since they return different parts of the place details.

Adi Levin
  • 5,165
  • 1
  • 17
  • 26
  • But what should the 'OpenPlaceInMapInteractor' do? Should it only return the coordinate? If that's the case, then they are returning the same thing (at least the 'RetrievePlaceInformationInteractor is returning the coordinate too) – Rodrigo Ruiz Feb 02 '16 at 17:42
  • I thought that you needed to return a redirect to some map app. No? – Adi Levin Feb 02 '16 at 19:14
  • it's an iOS app, not a web app. And the redirect is done via an iOS API. That API only needs a coordinate. – Rodrigo Ruiz Feb 02 '16 at 19:19
  • Then just return a coordinate. And if you think that the two use cases are simply identical and it is useless to have two use cases - just make one and call it "GetCoordinate". – Adi Levin Feb 02 '16 at 19:40