Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean Architecture: How to reflect the data layer's changes in the UI

Tags:

I'm trying to make a design based on the Uncle Bob's Clean Architecture in Android.

The problem:

I'd like to solve is how to make the changes generated in one repository to be reflected in other parts of the app, like other repositories or Views.

The example

I've designed a VERY simplified example for this example. Please notice that boundary interfaces has been removed to keep the diagrams small.

Imagine an app that shows a list of videos (with title, thumnail and like count), clicking a video you can see the detail (there you can like/dislike the video).

Additionally the app has an statistics system that counts the number of videos the user liked or disliked.

The main classes for this app could be:

For the Videos part/module: enter image description here

For the Stats part/module: enter image description here

The target

Now imagine you check your stats, then navigate the list of videos, open the detail of one, and click the like button.

After the like is sent to the server, there are several elements of the apps that should be aware of the change:

  • Of course the detail view, should be updated with the changes (this can be made through callbacks so no problem)
  • The list of videos should update the "likes" count for the given video
  • The StatsRepository may want to update/invalidate the caches after voting a new video
  • If the list of stats is visible (imagine a split screen) it should also show the updated stats (or at least receive the event for re-query the data)

The Question

What are the common patterns to solve this kind of communication? Please make your answer as complete as you can, specifying where the events are generated, how they get propagated though the app, etc.

Note: Bounties will be given to complete answers

like image 565
Addev Avatar asked Sep 26 '15 09:09

Addev


People also ask

What are the layers in clean architecture?

The layers are the main core of a clean architecture. In our app, we will use three layers: presentation, domain, and model. Each layer should be separated and shouldn't need to know about other layers. It should exist in its own world and, at most, share a small interface to communicate.

What is the advantage of using clean architecture in Android?

Advantages of Using Clean ArchitectureYour code is further decoupled (the biggest advantage.) The package structure is even easier to navigate. The project is even easier to maintain. Your team can add new features even more quickly.

What is clean architecture in programming?

Clean architecture is a software design philosophy that separates the elements of a design into ring levels. An important goal of clean architecture is to provide developers with a way to organize code in such a way that it encapsulates the business logic but keeps it separate from the delivery mechanism.


1 Answers

Publish / Subscribe

Typically, for n:m communication (n senders may send a message to m receivers, while all senders and receivers do not know each other) you'll use a publish/subscribe pattern. There are lots of libraries implementing such a communication style, for Java there is for example an EventBus implementation in the Guava library. For in-app communication these libraries are typically called EventBus or EventManager and send/receive events.

Domain Events

Suppose you now created an event VideoRatedEvent, which signals that a user has either liked or disliked a video. These type of events are referred to as Domain Events. The event class is a simple POJO and might look like this:

class VideoRatedEvent {     /** The video that was rated */     public Video video;     /** The user that triggered this event */     public User user;     /** True if the user liked the video, false if the user disliked the video */     public boolean liked; } 

Dispatch events

Now each time your users like or dislike a video, you'll need to dispatch a VideoRatedEvent. With Guava, you'll simply pass an instantiated event object to object to EventBus.post(myVideoRatedEvent). Ideally the events are generated in your domain objects and are dispatched within the persisting transaction (see this blog post for details). That means that as your domain model state is persisted, the events are dispatched.

Event Listeners

In your application, all components affected by an event can now listen to the domain events. In your particular example, the VideoDetailView or StatsRepository might be event listeners for the VideoRatedEvent. Of course, you will need to register those to the Guava EventBus with EventBus.register(Object).

like image 97
Fabian Keller Avatar answered Oct 14 '22 10:10

Fabian Keller