android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Apache License 2.0
15.52k stars 3.32k forks source link

Framework integrations #127

Open donlucard opened 8 years ago

donlucard commented 8 years ago

Hi,

can you share your thoughts where should framework specific stuff be placed. For example I have to integrate following:

Where should GCM code reside (InstanceIDListenerService and GcmListenerService)? How would you handle processing of incoming notifications/payload.

Where would you place tracking related classes and should we call tracker from presenter or usecase?

Trikke commented 8 years ago

Hi @donlucard ,

This depends on the scope of your project.

In smaller projects, stuff like this ends up in the Data layer. The interfaces to these infrastructures would be part of the Domain layer, but the implementation live in the Data layer. You could argue that you're still mainly handling data, so that's we i'd put it there. For analytics as an example, you would have a TrackEventUseCase with some parameters, which would flush these events to a cache in the Data layer. Once the cache is full, they are then flushed to Google Analytics. Or for example, if your app goes to the background, then all events are send to Google Analytics. This of course depends on your own requirements.

For larger projects, stuff like this could end up in a separate Infrastructure layer. The interfaces to these infrastructures would be part of the Domain layer, but the implementation live in the Infrastructure layer. Larger projects could end up being used on several platforms, so implementations for Push notifications and such would be different.

wilsoncastiblanco commented 7 years ago

Hey @Trikke, I have a question regarding framework integration, as far as I know Domain and Data layer have to be independent of frameworks, but, I want to know about your approach to applying it to a small project that I'm migrating to Clean Architecture.

I'm trying to create a login that gets the data from locally/remote, if the record is in the local DB the repository will bring the user from DB, if not, It will bring the data from a remote API, but, SQLite and Content providers belong to the Android Framework, so, at that point I'm breaking the Clean Architecture principle (?) because in the data layer the framework is in there, together with the Android context, used to get the instance of SQLiteOpenHelper and to get the ContentResolver for Content Providers, so, I would like to know your thoughts about that case, thank you!

kevin-barrientos commented 7 years ago

@wilsoncastiblanco remember that when you build a platform specific application, at some point you'll depend on those details. The idea is to abstract the details as much as we can without over engineering too much and end with an unreadable program. If you still want to abstract them you might do as @android10 said on one discussion:

Just something else to keep in mind when it comes to dealing with android components:

You can always decorate them so you do not depend directly on them. I did this with the main Scheduler thread: https://github.com/android10/Android-CleanArchitecture/blob/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/UIThread.java I do not want my domain logic to know anything about any strong dependency with the framework.