Java – Clean Architecture: Apps that rely heavily on background services

androidArchitectureclean codejava

I am trying to implement clean architecture (https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html) on an Android app.
As an example, let's say that all the app does is monitors a user's CPU temp and displays a hourly and daily average. Data is stored on the device and periodically downsampled. The downsampled data is uploaded to a backend.

All of the clean architecture examples I have seen is for the same types of apps – eg. Apps that have a UI to CRUD data, usually something in the line of: The UI sends click events to the presenter, presenter executes a use case interactor, interactor retrieves data from a repository, the presenter sends the data to the UI.

My app has practically zero user interaction:

  • A background service measures the CPU temp and writes it to the
    database.
  • A separate background process downsamples the data and writes it
    to the database.
  • Yet another background process syncs the downsampled data with the
    backend
  • The background services are scheduled to run on a fixed interval by a job scheduler
  • All the user sees is his current CPU, the averages and maybe
    notifications that syncing has completed successfully.

How would I structure this in terms of clean architecture? More specifically:

  • What are my use cases?
  • Are the services in the same layer as the view would have been?
  • Do I need a presenter at all? (Maybe for displaying status notifications from the service, I can't see any other reason?)
  • Do I need separate repositories for my high resolution and lower resolution data?

I realise this is a lot of questions, but I would appreciate any input or pointers in the right direction.

Best Answer

The important part of Clean Architecture (CA) are not the presenters and controllers. The important point of CA is that there is "core" of your application, which represents your business cases. And that all of UI, infrastructure, service or databases depend on this core and not vice-versa. I recommend wathing Uncle Bob's talk to get clearer picture.

When I look at your code, youre "core" use case is a pipeline. This pipeline consists of multiple parts, each run separately with database in between them. But together, they do some processing. What I would like to see is automated test that tests the whole pipeline as singular unit. The database and scheduler would be mocked out and the pipeline would not know if it is running in production or if it is being tested with in-memory database and run directly and not through scheduler.