|
| 1 | +# Use Cases |
| 2 | + |
| 3 | +Use Cases can be both asynchronous and synchronous. |
| 4 | + |
| 5 | +The primary difference is that a synchronous Use Case will return it's result, and an asynchronous Use Case will call a callback with it's result. |
| 6 | + |
| 7 | +It is possible to generalise the boundary interfaces of these two types of Use Cases. |
| 8 | + |
| 9 | +## Asynchronous Example |
| 10 | + |
| 11 | +Asynchronous use cases provide greater control over rendering to the UI. However they add complexities to testing. |
| 12 | + |
| 13 | +An [example generalisation can be found here](https://github.com/madetech/dojos/blob/master/mld-klean-architecture/src/main/kotlin/com/madetech/clean/usecase/AsynchronousUseCase.kt), |
| 14 | +with [a derivative here](https://github.com/madetech/dojos/blob/master/mld-klean-architecture/src/main/kotlin/io/continuousfeedback/core/usecase/CreateTeamMember.kt) for a specific use case. |
| 15 | + |
| 16 | +```kotlin |
| 17 | +package com.acmeindustries.widget.usecase |
| 18 | + |
| 19 | +interface ViewWidgets { |
| 20 | + fun execute(request: Request, presenter: Presenter) |
| 21 | + |
| 22 | + data class Request(...) |
| 23 | + interface Presenter { |
| 24 | + fun onSuccess() |
| 25 | + fun onError() |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | +## Synchronous Example |
| 32 | + |
| 33 | +Synchronous Use Cases provide a simpler interface for testing, but can make representing failure paths and control over the UI harder to maintain. |
| 34 | + |
| 35 | +```kotlin |
| 36 | +package com.acmeindustries.widget.usecase |
| 37 | + |
| 38 | +interface ViewWidgetPerFooBarReport { |
| 39 | + fun execute(request: Request): Response |
| 40 | + |
| 41 | + data class Request(public val fromDate: String, public val endDate: String) |
| 42 | + data class Response(...) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +```kotlin |
| 47 | +package com.acmeindustries.widget |
| 48 | + |
| 49 | +import com.acmeindustries.widget.usecase.ViewWidgetPerFooBarReport |
| 50 | +import com.acmeindustries.widget.usecase.ViewWidgetPerFooBarReport.* |
| 51 | +import com.acmeindustries.widget.domain.Widget |
| 52 | + |
| 53 | +class WidgetPerFooBarReport(val widgetGateway: WidgetGateway) : ViewWidgetPerFooBarReport { |
| 54 | + fun execute(request: Request): Response { |
| 55 | + val widgets = widgetGateway.all() |
| 56 | + //secret sauce here |
| 57 | + return Response(...) //return response populated with data |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +interface WidgetGateway { |
| 62 | + fun all(): List<Widget> |
| 63 | +} |
| 64 | +``` |
0 commit comments