Latest / DevQuestions with Tim Corey / 167 Should I Create Minimal APIs or Full APIs?
Transcript
- 0:00SPEAKER_00: Welcome to the Dev Questions Podcast with Tim Corey.
- 0:03SPEAKER_00: Join us each episode as we tackle the questions you are asking about a career in software development, understanding the industry, and new technology.
- 0:12SPEAKER_00: If you're just starting out or you want to grow stronger as a developer, this is the place to get your questions answered.
- 0:19SPEAKER_00: Now, here's your host, expert developer and online educator, Tim Corey.
- 0:26SPEAKER_02: Should I create a minimal API or a full API?
- 0:29SPEAKER_02: That's the question for today's episode of DevQuestions.
- 0:33SPEAKER_02: Now, if you have a question you'd like to see answered, go to suggestions.iamtimcorey.com.
- 0:39SPEAKER_02: Now, this question has come up a lot recently because of the work I've done on my YouTube channel on minimal APIs.
- 0:46SPEAKER_02: And so the question is, why do we use minimal APIs when we already have an API structure?
- 0:52SPEAKER_02: What's the difference?
- 0:53SPEAKER_02: What's the point of them?
- 0:55SPEAKER_02: And the simple answer is that it's up to you when you use minimal APIs versus full APIs.
- 1:04SPEAKER_02: Think of it like having two different screwdrivers that are maybe a little different size, but they're for the same type.
- 1:11SPEAKER_02: Well, why'd you have two?
- 1:13SPEAKER_02: Well, because one might fit your situation a little better.
- 1:17SPEAKER_02: And so having two gives you options.
- 1:20SPEAKER_02: It gives you a choice in the matter.
- 1:22SPEAKER_02: The original purpose of a minimal API was microservices.
- 1:27SPEAKER_02: The idea that we could write a very, very small API.
- 1:31SPEAKER_02: There's no reason then to have a separate controller file and separate model file and separate all this stuff out when it could all really be in one page and be simpler.
- 1:44SPEAKER_02: Or a small API, or maybe you have one controller but four or five actions on that.
- 1:50SPEAKER_02: Well, that would be a good choice for a minimal API instead, where again, maybe it's still one file separated out, but it's just one, you know, or five endpoints.
- 2:01SPEAKER_02: That's kind of simple.
- 2:03SPEAKER_02: But let's talk now about, you know, some of you are already thinking your mind, I'm pushed back on the idea, well, but controllers are there for a reason.
- 2:11SPEAKER_02: We have controllers and they're a good pattern.
- 2:14SPEAKER_02: Let's talk about design patterns for a minute.
- 2:17SPEAKER_02: Because often design patterns are misunderstood.
- 2:22SPEAKER_02: And yes, this is relevant to APIs.
- 2:24SPEAKER_02: Because the API has this pattern of creating controllers and separating controllers out from the models and separating that out from the startup logic and having all this separation.
- 2:37SPEAKER_02: But what's the point of a pattern?
- 2:40SPEAKER_02: A pattern's goal is to ease maintainability and it's to ease readability.
- 2:48SPEAKER_02: So it makes it easier to read and it makes it easier to maintain.
- 2:52SPEAKER_02: And if you think through the different design patterns that you know about, it feels like they don't do that.
- 2:59SPEAKER_02: It feels like they make things more complex.
- 3:02SPEAKER_02: And the reason why is because you're applying them too early.
- 3:07SPEAKER_02: The goal is that you apply something that may be somewhat complex because your code is more complex or will be more complex.
- 3:18SPEAKER_02: And so this pattern might be complex, but it's easier and easier to maintain and easier to read than the alternative.
- 3:27SPEAKER_02: But when the alternative is simpler, guess what?
- 3:32SPEAKER_02: You shouldn't apply that pattern.
- 3:35SPEAKER_02: I have an example on YouTube where I create the perfect console application.
- 3:40SPEAKER_02: I have a big asterisk on the screen because it's a it's a lot of code.
- 3:45SPEAKER_02: And it's good code, and it's code that in the right situation is awesome.
- 3:50SPEAKER_02: But the end result is it's an application that says hello world.
- 3:55SPEAKER_02: Did it need to be multiple files and different dependency injection and all the rest of this good stuff?
- 4:03SPEAKER_02: Logging, did it need to have all this stuff?
- 4:06SPEAKER_02: Well, no, because it was just gonna say console.write line, hello world.
- 4:12SPEAKER_02: That's it.
- 4:13SPEAKER_02: Well, that's the kind of the point here is when you have something so simple, you don't need to add a pattern to it.
- 4:21SPEAKER_02: You don't need to separate out files.
- 4:23SPEAKER_02: You don't need to have the complexity because it's so simple.
- 4:27SPEAKER_02: And that's where, again, microservices and small APIs can benefit from this minimal API pattern or minimal API structure because it is much simpler and much less patterns or cruft around the code.
- 4:46SPEAKER_02: So it's much more just here's the code, here's, you know, here's the startup code, and then down here is you know the actual API code, three or four, five endpoints, and that's it.
- 4:58SPEAKER_02: Well, that can be a one file, and it's okay.
- 5:02SPEAKER_02: So microservices, small APIs really benefit from the idea of minimal APIs.
- 5:10SPEAKER_02: Now, if you structure them properly, larger APIs can also benefit from minimal API design.
- 5:18SPEAKER_02: Again, minimal APIs are designed to be just what you need and simple to read and simple to use.
- 5:26SPEAKER_02: Again, easy to maintain and readable.
- 5:30SPEAKER_02: Well, even with a growing structure of your API, you can still add out and add more things, but not have to go to the controller model because maybe that's not the right call for your specific situation.
- 5:43SPEAKER_02: You can grow out a minimal API that is still clean even after you have 30 and 40 different endpoints.
- 5:53SPEAKER_02: So there are ways of doing it and ways to grow a minimal API to a pretty large size, and ways to make a controller-based API smaller.
- 6:05SPEAKER_02: So there's a lot of overlap here, which is why, again, I come back to it kind of depends on what you want to do.
- 6:12SPEAKER_02: You get the choice of what you think is best for your situation, for your code base, and for your developers.
- 6:20SPEAKER_02: Now, there is no need to convert controllers over to endpoints.
- 6:26SPEAKER_02: You don't have to do that.
- 6:27SPEAKER_02: Leave them.
- 6:28SPEAKER_02: It's just a different tool.
- 6:30SPEAKER_02: You can have more than one tool in your toolbox.
- 6:33SPEAKER_02: Don't just always look for the best tool.
- 6:35SPEAKER_02: Look for the tool that works best for your particular situation.
- 6:39SPEAKER_02: And the next time a situation comes up, evaluate again.
- 6:44SPEAKER_02: If you did want to update, fine, cool.
- 6:47SPEAKER_02: That's up to you.
- 6:48SPEAKER_02: And it may work well if you're looking to go from, say,.NET Framework API over to a.NET 7 API.
- 6:56SPEAKER_02: So you can upgrade if you want or change if you want, but really choose between minimal APIs and controller based really depends on your situation and what you find best for your code base.
- 7:10SPEAKER_02: Okay?
- 7:10SPEAKER_02: So that's my answer to the question.
- 7:12SPEAKER_02: I hope that answers it.
- 7:14SPEAKER_02: If not, or if you have more questions, go to suggestions.iamtimcorey.com.
- 7:19SPEAKER_02: Thanks for listening, and as always, I am Tim Corey.
- 7:22SPEAKER_01: Thank you for joining us for this episode of Def Questions.
- 7:25SPEAKER_01: Tim is committed to making it easier for you to become a developer.
- 7:29SPEAKER_01: If you would like to help make more content like this possible, please like, subscribe, rate, and share Deaf Questions.
- 7:36SPEAKER_01: You can also send your questions to questions at IamTimcorey.com.
- 7:41SPEAKER_01: Until next time, remember, you are too smart and your time too valuable to waste it making all the mistakes Tim did.
- 7:48SPEAKER_01: When you're ready to learn to think and code like a professional developer, head over to imtimcore.com and enroll in a course.