Adding a domain layer. In this article, I’ll explain how we… | by Don Turner | Android Developers | Dec, 2022 | Power Tech

Posted on

On this article, I am going to clarify how we added a website layer to the Now in Android app to enhance readability, scalability, and portability.

The area layer sits between the consumer interface and the information layers.

Within the Android software structure, the area layer accommodates the enterprise logic: the foundations that dictate how the appliance works. In the remainder of this text, I am going to use the time period “logic” to imply “enterprise logic,” versus “consumer interface logic” or some other type of logic.

It’s common to introduce the area layer as an software grows in complexity and use it to encapsulate advanced logic or logic that’s reused by many screen-level state holders, resembling ViewModels.

The pull request on which this text relies is right here. I’ve simplified and renamed some courses to deal with the fundamentals.

The area layer is created by shifting logic, usually from the consumer interface layer, to use instances. Use instances are features (or courses with a single public methodology) that include logic. They carry out a single operation that usually combines or transforms information from repositories or different use instances.

The naming conference to be used instances on this article (and within the Now in Android app) follows the official steerage from:

  • current tense verb e.g Get
  • noun/what e.g FollowableTopic
  • UseCase suffix.

Instance: GetFollowableTopicUseCase

Right here is an summary of the method we use:

  • Establish duplicate and complicated logic inside ViewModels
  • Create use instances with acceptable names
  • Transfer logic inside use instances
  • Refactor ViewModels to depend upon use instances as an alternative of repositories
  • Add assessments to be used instances

The next diagram exhibits the information noticed by every ViewModel. Every field within the Observes column represents logic, which usually combines information from a number of streams. Every field represents a candidate for a use case, and people with the identical colour point out duplicate logic.

Diagram exhibiting the logic that every view mannequin accommodates and the candidate use instances for that logic

The “UseCase” suffix is ​​omitted from the diagram for readability.

Now that the laborious a part of “naming issues” is finished, we simply want to maneuver the logic from every ViewModel to its corresponding UseCase. Let’s check out an instance.

The logic for information articles is utilized in three totally different ViewModels.

Diagram exhibiting duplicate logic in three ViewModels shifting to a single use case

Let’s check out this internal logic. BookmarksViewModel:

That is what is going on:

  1. Markers are obtained from UserDataRepository
  2. Information assets (also referred to as articles) are sourced from NewsRepository
  3. These two are mixed to create an inventory of bookmarked information assets
  4. The listing is filtered to indicate solely the marked information assets

The logic from steps 1-3 is widespread to all different ViewModels, so it may be moved to a brand new use case known as GetSaveableNewsResourcesUseCase. Right here is the code:

The use instances dwell within the area layer, and to obviously separate this layer from the remainder of our code base we created a :area module. Lessons created by use instances, often as a result of combining information fashions from multiple repository, had been additionally moved to the :area module.

A superb instance of that is the SaveableNewsResource information class that’s the results of combining a NewsResource provided from the NewsRepository forks isSaved property that’s calculated utilizing the listing of bookmarks from the UserDataRepository.

Diagram exhibiting the information layer courses (NewsResource and UserData) being mixed into one area layer class (SaveableNewsResource).

Now we have now created GetSaveableNewsResourcesUseCase we are able to refactor BookmarksViewModel to name him

ViewModel is now less complicated, simpler to learn. It is clear to the constructor what this class is doing: get saveable information assets, and there isn’t any want for a intermediary. bookmarks variable to retailer information from the consumer information repository.

There’s nonetheless some logic to filtering out unflagged information articles. We might have moved this filtering logic to a different use case (maybe known as GetSavedNewsResourcesUseCase) however creating a completely new class for a name to filter in all probability does not justify the elevated complexity of a further use case. Finally, it is as much as you to resolve how a lot logic to translate into your use instances.

The opposite ViewModels can now be refactored to depend upon our use case. Including any widespread logic, resembling the flexibility to return information assets for a given subject or creator, as we go.

We repeat this course of for every duplicate logic space:

  • select a view mannequin
  • transfer the logic to a use case
  • refactor the opposite ViewModels to make use of the use case

In fact, we additionally add assessments for every use case, and since use instances are easy features, they’re very straightforward to check! Finally, by including a website layer, our code base is less complicated to take care of and scale.

You’ll be able to learn extra in regards to the area layer in Now on Android on the Structure studying journey and a extra detailed information on the official Android developer web site.



Adding a domain layer. In this article, I’ll explain how we… | by Don Turner | Android Developers | Dec, 2022