This week's newsletter is sponsored by Milan Jovanovic
Pragmatic Clean Architecture: Learn how to confidently ship well-architected production-ready apps using Clean Architecture.
Milan used exact principles to build large-scale systems in the past 6 years.
In today’s newsletter we are going to discuss:
Minimal APIs Introduction
Pros and cons of using them
When should we use them
Practical Demonstration
Replace controllers with Minimal APIs
Dependency Injection
Making Program.cs file cleaner by moving code
Minimal APIs Introduction
Minimal APIs were introduced were introduced in .NET 6 and more light weight than controllers.
Minimal APIs are a simplified approach for building fast HTTP APIs with ASP .NET Core
Pros and Cons
These are the benefits of using Minimal APIs:
Minimal APIs offer a more concise and focused syntax for defining routes, endpoints, and handling requests.
Traditional controllers often require a significant amount of boilerplate code to set up routes, actions, and parameter bindings. With Minimal APIs, this boilerplate is significantly reduced.
Minimal APIs use a functional-style syntax that allows you to define routes and endpoints using lambda expressions.
For small to medium-sized APIs, you can define your entire API in a single file, making it easier to manage and navigate your codebase.
Minimal APIs can speed up development cycles due to less code.
Minimal APIs allow you to define routes and actions without the need for separate controller classes.
Additionally Minimal APIs have some cons:
For complex scenarios where we need to put a lot of routes Minimal APIs are less flexible alternatively they are best fit for those Microservices where we create tiny APIs.
Testing can become difficult while using Minimal APIs because it is not as simple as we do for controllers.
We might face lack of resources/community/tools/libraries while working with Minimal APIs because these are quite new in .NET world
Replacing Controllers with Minimal APIs
We can use following methods of Web Application for CRUD operations:
MapGet
MapPut
MapPost
MapDelete
Each method takes two parameters a string(which is route path) and delegate(which could be anonymous type or strong type and empty as well)
Let’s look how its controller looks would be:
Now we will replace it with Minimal APIs , instead of putting all this code in different controller file , we would put our code in Program.cs file.
How to inject dependencies using Minimal API
Let’s update above example and now add IConfiguration in our API and retrieve a hardcoded username from appsetting (Just for the sake of demo).
Sometimes we need to inject IConfiguration
or IHttpClientFactory
then we can use it like this:
How to make your Program.cs file cleaner
It seems fine if we have just 4-5 minimal APIs but what if you have larger number then Program.cs file would become messy and lengthy creating difficulties in managing it.
Create different method that handles delegate part (As I mentioned above Minimal APIs take route and delegate) then call that method. But still code would be in Program.cs file which I don’t want.
We can create regions as well to differentiate APIs aligned with a specific entity but it does not make our Program file cleaner.
Let’s create a extension method for each entity endpoints:
Now register it in Program file
Additionally you can check this video on “How To Organize Minimal API Endpoints Inside Of Clean Architecture”.
For more in depth knowledge check official docs of Microsoft
When should we use Minimal APIs
Simple application with a few routes
Microservices (with small number of routes)
GitHub Code
You can find code of this newsletter issue at this GitHub Repository
Whenever you’re ready, there are 3 ways I can help you
Promote yourself to 7300+ subscribers by Sponsoring my Newsletter
Become a Patron and get access to 140+ .NET Questions and Answers
Get a FREE eBook from Gumroad that contains 30 .NET Tips (2800+ downloads)
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez