|
7 | 7 |
|
8 | 8 | == Demonstration |
9 | 9 |
|
10 | | -Let's take a look at the example. |
| 10 | +Let's take a look at the following example. |
11 | 11 | We'll be looking at a Cabs ride-sharing application, that mimics real-world solutions of similar kind. |
12 | 12 | The application is written in a popular Java framework. |
13 | 13 |
|
14 | 14 | === Initial application |
15 | 15 |
|
16 | | -Here's a method from the Cabs application that handles the completion of transit: |
17 | | - |
18 | | -[WARNING] |
| 16 | +[IMPORTANT] |
19 | 17 | ==== |
20 | | -The following example should be considered an anti-example! |
| 18 | +The following example should be considered as suboptimal, most likely a counterexample! |
21 | 19 | ==== |
22 | 20 |
|
| 21 | +Here's a method from the Cabs application that handles the completion of a ride. |
| 22 | + |
23 | 23 | [source,java] |
24 | 24 | ---- |
25 | 25 | @Transactional // <1> |
@@ -51,16 +51,53 @@ public void completeTransit(Long driverId, UUID requestUUID, Address destination |
51 | 51 | } |
52 | 52 | ---- |
53 | 53 |
|
54 | | -There are a number of problems with that method: |
| 54 | +[CAUTION] |
| 55 | +==== |
| 56 | +There are issues with the above method. |
| 57 | +
|
| 58 | +<1> It uses the `+@Transational+` annotation, and modify number of unrelated data stores. |
| 59 | +// This means that when one of those operations fails, the whole processing will be rolled back. |
| 60 | +// In effect, the end-user will receive a nasty error message. |
| 61 | +
|
| 62 | +<2> It merges different, business domains. |
| 63 | +// This makes it hard to understand and maintain. |
| 64 | +// It isn't required to all of those operations complete at the same time. |
| 65 | +==== |
| 66 | + |
| 67 | +Similar methods are, unfortunately, quite common in business applications. |
| 68 | +At first glance, many developers don't see any problems with similar code. |
| 69 | +Let's break down the problems in detail. |
| 70 | + |
| 71 | +==== Overuse of transactional processing |
55 | 72 |
|
56 | | -<1> It uses the `+@Transational+` annotation, and modify number of data stores. |
| 73 | +The transactional processing has been the cornerstone of many business applications. |
| 74 | +However, in most cases, the transactional processing isn't the best fit for real-world processes. |
| 75 | + |
| 76 | +In our example, when the ride finishes, that's a real-world situation. |
| 77 | +However, the example uses the `+@Transational+` annotation, and operate on number of unrelated data. |
57 | 78 | This means that when one of those operations fails, the whole processing will be rolled back. |
58 | 79 | In effect, the end-user will receive a nasty error message. |
59 | | -<2> It merges different, business domains. |
60 | | -This makes it hard to understand and maintain. |
61 | | -It isn't required to all of those operations complete at the same time. |
62 | 80 |
|
63 | | -Include here content related to potential existing demos: blogs, articles, recorded videos, walkthrough guides, tutorials. |
| 81 | +[NOTE] |
| 82 | +==== |
| 83 | +Outages from the dependant services must not invalidate the main intent. |
| 84 | +In fact, all the operations in this example could happen independently, and at different, yet reasonable times. |
| 85 | +==== |
| 86 | + |
| 87 | +==== Bundling of different logical domains |
| 88 | + |
| 89 | +Our example is also very chatty, and hard to understand at first glance. |
| 90 | +In fact, this is quite common in similar applications. |
| 91 | +The code starts small, easy to understand. |
| 92 | +When new features are added, it keeps growing as developers cramp new instructions into methods like `+completeTransit+`. |
| 93 | + |
| 94 | +Of course, the developers could re-architect the code, to extract instructions to a separate blocks, but that is just a half-measure. |
| 95 | +Still, the application will do all the operations, starting from `+completeTransit+` method in the same time, and withing the same _"script"_. |
| 96 | + |
| 97 | +=== Refactoring |
| 98 | + |
| 99 | +In this section, we'll refactor the Cabs application. |
| 100 | +The refactoring will be limited to make the process easy to understand. |
64 | 101 |
|
65 | 102 | image::https://www.plantuml.com/plantuml/svg/VP1DJiCm58JtFiMZ-rmWYwgqeHkeX2WNUBK7Ok4ubdyYzVQuZKbe5TZ5olTcFiqcHFOnTKOyn1OTIC8d0xPLdwBH5iBb_rfgnpRIwWMVBC_qwDoAED3ul4MUBKSzW9u6vES1eRsYMzz_mT-YZS-W3tJeLUwyOdlW23zeYJkK8vyuZ52p5O9bRk687uTYLgrB4zNqcav6XvPsR6GocTsZQ8d2L1aV3slQzVP3-uuKpCNgB1JkEwQpzI_FcjxoL5XgcUvdMioVL4soi-iuIOQcE5N259RYPgKYMNJ-3lfdkMPRqp7s7lJkjQFBvWihR61Lwimt[width=100%] |
66 | 103 |
|
|
0 commit comments