Stop Adding a Service Layer to Every Web API

Stop Adding a Service Layer to Every Web API

Zoran on C#

0:00 In this video I will show you one design

0:04 mistake which I see very frequently in practice,

0:07 namely, many programmers firmly believe that every every Web

0:12 API they design requires a service layer.

0:17 That is not true.

0:19 Anyway, the confusion exists.

0:21 I don't know why and where that came from.

0:24 And so, in this video, I will show you one

0:26 small Web API in which I plan to show you how you make that decision,

0:32 whether there is the need for a service layer or not.

0:35 And the decision will be very easy

0:37 to make in the end.

0:39 This is an endpoint

0:40 implementation in a simple Web API.

0:42 It is typical in many respects.

0:45 The endpoint receives a request DTO, validates the DTO against the

0:50 database to produce a civilized error report.

0:52 This is not necessary, but it's nice to help.

0:56 Eventually, it does the work,

0:58 saves changes to the underlying database, and, very importantly,

1:03 prepares the response DTO.

1:05 Any other command in this

1:07 Web API would have the same structure.

1:11 Validate against the database, do the work,

1:14 update the database, construct the response.

1:17 Queries are different, in the sense that

1:20 they are simpler.

1:21 And that's it.

1:22 What can we say about this API?

1:26 What I see at first glance is

1:29 that it writes into a single database.

1:34 It might read from many data sources,

1:38 but it writes into one.

1:40 It never writes into two independent data sources.

1:44 The key term which I will introduce at this point is that none of these

1:51 endpoint implementations requires an orchestration

1:55 of different mutable entities.

1:58 Nevertheless, let's assume that we have decided to implement the service

2:05 layer, and I will do that right now.

2:09 There will be the services namespace.

2:13 There would be an abstract service that manages company data.

2:17 It would create a new company,

2:19 update the fields of a company, then manage addresses of a company,

2:24 support searching the companies.

2:26 Those are the operations the company

2:30 service must implement.

2:31 But the choice of these

2:33 operations is not accidental.

2:36 Each of these abstract methods

2:39 implement one transactional boundary, and,

2:42 by doing that, it implements one use case.

2:46 And so, I will specify the responsibilities of this service.

2:50 It is defining and managing transactional boundary.

2:53 It is also responsible to enforce authorization rules, but not authentication.

3:00 On the technical level, it would support asynchronous execution.

3:06 It would support cancellation, too.

3:09 You see, my beautiful set of abstract

3:13 methods becomes to grow larger now.

3:16 But there is more, the service must

3:20 also return meaningful errors.

3:22 More responsibilities remain.

3:24 I should have defined error model and separate input output DTOs.

3:29 The service layer should speak its own language,

3:33 not to reuse the language of the layer above it.

3:37 That is a design flaw.

3:39 Now, is this a good design?

3:42 The bloat is sneaking in, but still,

3:45 I do have those principal elements, like managing

3:49 transactional boundaries.

3:50 That is very important.

3:52 I also have the separation of use cases, one per method, that is very good.

3:59 But, is this a good idea?

4:01 The best way to see how good

4:04 an interface is, is to use it.

4:08 I will reimplement one endpoint,

4:10 the one which updates a single company address,

4:13 and that will finally tell us when it is a good

4:18 idea to introduce the service layer, and when it is just a waste of time.

4:23 Before I show you this final revelation, let me remind you that everything on

4:28 this channel is free, thanks to the patrons.

4:31 If you like what you have learned from this video

4:34 and from all other videos on my channel, then,

4:37 please join the community on my Patreon page and

4:40 help me maintain this channel free forever.

4:43 Thank you very much.

4:45 Now, the endpoint would receive the application service as a dependency.

4:50 The remaining arguments are the parameters to the use case.

4:54 You cannot avoid them.

4:56 And then, it begins.

4:59 We would delegate the use case to the service layer, analyze errors,

5:03 and return bad request, if any happened.

5:07 This is very simplified approach to errors.

5:12 I don't even think it is correct entirely.

5:14 If no errors occurred,

5:16 we would return the Ok result.

5:19 Now, is this how you would use a service?

5:23 What do you think?

5:27 No, not really.

5:29 First, the service would not use the same DTOs which the Web API is using.

5:36 Those are two different layers.

5:38 I would have to map every

5:40 DTO before returning the response.

5:42 The errors are not just a list of strings, oh.

5:48 There would be a translation from the errors model to an actual HTTP response,

5:54 including possibly localization.

5:55 It's far from trivial.

5:58 And of course,

5:59 there would also be a translation of input DTOs into whatever DTOs the service

6:04 layer is using.

6:06 Therefore, it's not possible to just use the service.

6:11 There is a lot of boilerplate code around it.

6:15 Every service comes with boilerplate.

6:17 You need to sustain that.

6:19 You need to implement that code to be able to use the service or,

6:24 again, why?

6:25 is there a reason to do that?

6:29 For a Web API to have a reason, it would need

6:34 to depend on another mutable service.

6:38 And another one possibly.

6:41 It would need to orchestrate changes in multiple places.

6:48 For example, changing your local database, but also managing the

6:52 payment gateway or something like that.

6:55 You would definitely need to orchestrate

6:58 things that happen in two systems.

7:00 I don't have that in my Web API.

7:04 And, quite often, you won't have two mutable, transactionally independent

7:10 systems to orchestrate in your Web API.

7:14 You might be reading from multiple data sources.

7:19 But that's a completely different thing.

7:21 You're not changing them.

7:22 You're changing only one.

7:24 Also, when there is one-on-one

7:27 relationship between the Web API and the service layer,

7:32 then even the DTOs are an exaggeration.

7:36 Errors are much simpler in that case, too.

7:41 So I don't need any of this added bloat.

7:46 In the end, this would be my endpoint implementation.

7:50 Just check whether there is an error and return bad request,

7:55 or otherwise return Ok and the

7:58 object that is the result of the operationation.

8:01 Most of the web APIs and components and services

8:05 you will ever write, look exactly like this.

8:10 Only sometimes, the reasons for a separate service

8:15 layer will be satisfied, just as I've shown you in this video.

8:21 I told you, the decision will be easy to make in the end.

Study with Looplines Download Captions Watch on YouTube