Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Architecture in Action [part 1] - Saeed M...

Saeed Masoumi
September 13, 2018

Clean Architecture in Action [part 1] - Saeed Masoumi

Saeed Masoumi

September 13, 2018
Tweet

More Decks by Saeed Masoumi

Other Decks in Programming

Transcript

  1. What we learned • The mindset behind the clean architecture

    • The dependency rule • Testability (the most important part) • Separation of concerns • Level of abstraction
  2. Introducing Android Jetpack • Robust • Solve many known problems

    • Being sold as a silver bullet • Kotlin friendly
  3. Kotlin • AndroidX • Kotlin Android Extension plugin • Replacement

    of some famous annotation-based processing libraries such as Dagger, Butterknife, … • DSL (Gradle) • Functional (Arrow) • Coroutines
  4. Clean Architecture on Android • What happen when we implement

    clean architecture on real productions? • YAGNI • Interactors act just as a proxy to a gateway • Do we really need abstraction? • Kotlin features instead of (some) abstractions • Redundant boilerplate • Most of the time, we’re dealing with platform specific concerns
  5. Clean Architecture on Android • MV ( ? ) •

    Dagger Hype • Using Kodein and Koin with no more code get • RxJava Hype • RxJava vs Kotlin coroutines + kotlin operators vs LiveData • Memory consumption
  6. Clean Architecture on Android • Testing is forgotten because of

    time: • Which MV(?) ? • Dagger or Koin? • RxJava or Coroutines? • Clean architecture boilerplate • And many other things …
  7. Clean Architecture on Android • Sometimes our platform specific codes

    are not clean enough • Location and GPS modules • The logic inside Notification, Download manager, Background Job and … • Communication between these things and our data or presentation layer ——> Side effect on testing
  8. It’s not just about Architecture • Making an app contains

    different criteria and concerns • Architecture must be aligned with testing • Architecture is to support the life cycle of the system
  9. There is no silver bullet • It always depends on

    • Using Rx or Coroutines • Using interactors or not • Which test framework • ….
  10. Different components of an application Business Logic Decision Architecture Design

    CI/CD Build System Cross-cutting Concern Test App Implementation SAST Platform Specific
  11. Android SDK, RxJava, Dagger, Robolectric, … Business Logic Decision Architecture

    Design CI/CD Build System Cross-cutting Concern Test App Implementation SAST Platform Specific
  12. Logging, Analytics, Developer Settings …. Business Logic Decision Architecture Design

    CI/CD Build System Cross-cutting Concern Test App Implementation SAST Platform Specific
  13. Download Manager, Job Scheduling, … Business Logic Decision Architecture Design

    CI/CD Build System Cross-cutting Concern Test App Implementation SAST Platform Specific
  14. fastlane, gradle, lint, sonarqube , … Business Logic Decision Architecture

    Design CI/CD Build System Cross-cutting Concern Test App Implementation SAST Platform Specific
  15. What we are going to do? • Aggregate all these

    concerns • Making a multi-platform app • Mobile, Wear and TV • Based on TV Maze API • Clean + Kotlin + AAC + Test + CI/CD + Build system .…. • Available on Github ( SaeedMasoumi/MovieHub )
  16. Our Stack • Kotlin • AAC, Leanback, Wearable • Koin

    • Glide • (RxJava, Coroutines) Maybe ?! • Retrofit • Circle CI • Espresso, Robolectric , Mokk • Lint, Detekt, Ktlint, SonarQube, … • Developer Settings (Leak Canary, Logs, …)
  17. Modularity • Modularity is being popular • Different way to

    modularize your app • Based on layer • Based on feature
  18. Business Logic • Identify our requirements • Create entities •

    Create our domain layer (Repositories/Services, Interactors, …) • Create any other abstractions
  19. Business Logic data class TVShow( val id:Int, val name:String, val

    summary:String, val schedule:Schedule, val tvNetwork: TVNetwork, val url:String //... )
  20. Business Logic data class TVShow( val id: Int, val name:

    String, val summary: String, val schedule: Schedule, val tvNetwork: TVNetwork, val url: String //... ) data class TVNetwork( val id: Int //... ) data class Schedule( val time: Date //... )
  21. Business Logic interface TVShowRepository { fun getAllShows(): List<TVShow> } interface

    FavoriteRepository { fun add(showId:Int): List<TVShow> }
  22. AAC on Wear public class MyActivity extends WearableActivity implements LifecycleOwner

    { private LifecycleRegistry mLifecycleRegistry; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLifecycleRegistry = new LifecycleRegistry(this); mLifecycleRegistry.markState(Lifecycle.State.CREATED); } @Override public void onStart() { super.onStart(); mLifecycleRegistry.markState(Lifecycle.State.STARTED); } @NonNull @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } }
  23. AAC on TV public class SearchFragment extends android.support.v17.leanback.app.SearchSupportFragment implements android.support.v17.leanback.app.SearchSupportFragment.SearchResultProvider{

    private VideosViewModel viewModel; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //TODO implement viewModel.getSearchResult().observe(…) } }
  24. Data <-> Remote & Local • Using datasources to switch

    between sources • Decision goes into data layer • Room and DAOs goes into local layer (AKA cache, persistence, storage, …) • Retrofit goes into remote • Any other logics (e.g. Location) goes into data layer or if you need more modularity make a separate module
  25. Summary • AAC • Kotlin Features • Clean Architecture Revision

    • Other aspects of Apps ( CI/CD, Test etc) • No Silver Bullet