Unveiling the Elegance and Efficiency of Minimal APIs

Introduction

Minimal APIs are designed to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.

What are Minimal APIs?

Minimal APIs are a programming approach that focuses on simplicity, conciseness, and efficiency when creating application programming interfaces (APIs). This methodology aims to streamline the API design and implementation process by minimizing unnecessary code, reducing complexity, and prioritizing the API's core functionalities.

Minimal APIs are often associated with frameworks and tools that enable developers to create APIs with minimal configuration and boilerplate code. These frameworks provide a more straightforward and intuitive way to define endpoints, routes, and actions, allowing developers to concentrate on the essential aspects of their APIs without being burdened by repetitive or redundant code.

Using Minimal APIs in .NET 6 Or Higher offers an alternative approach to building APIs. The main advantage is that it provides a lightweight foundation compared to MVC. This can often lead to improved performance in various scenarios.

Example of the Minimal APIs

Let`s Take an example of the Minimal APIs.

To Create a simple Minimal API that displays “Hello world!”, you need to define your API in .Net core 6 or higher project with MapGet if the method Get verb, I`m using .Net core 7 in the following example.


var app = builder.Build();

app.MapGet("/", () => "Hello world!"); //This line will display “Hello world!” in the home page.

app.Run();

you should define the API route and then the implementation. Also, you can separate the implementation of the code in a static method and call it inside the API.

var app = builder.Build();

app.MapGet("/", GetText); //calling the method here

app.Run();

static string GetText() => "Hello World!";

In addition to this, you can use Post and Put and other verbs with MapPost Or MapPut, etc.

Parameters in Minimal APIs

We can specify Parameters when you do the API implementation. Let's take a look at the below API to get id, for example.

var app = builder.Build();

app.MapGet("/{id}", GetId); //Here we specify that the api hits only if you send a parameter

app.Run();

static string GetId(string id) => id; //Here the type of the parameter

Dependency Injection in Minimal APIs

Dependency injection in minimal APIs allows you to easily manage and inject dependencies into your application, similar to how it is done in traditional ASP.NET Core applications. However, the approach is more streamlined and minimalistic. This means that you can efficiently handle and use dependencies in your minimal APIs, making your code more organized and maintainable.

Let`s take an example:

Firstly, I created a service called TextService that includes the “GetText” method and inherits the ITextService interface.

public class TextService : ITextService
{
    public string GetText() => "Hello world!";
}

Now, we can use the DI.

builder.Services.AddScoped<ITextService, TextService>(); //Here I register my service and interface

var app = builder.Build();

app.MapGet("viewText", HandleGetText); //Calling HandleGetText method.

string HandleGetText(ITextService textService) => textService.GetText(); /*Here you can inject the 
service (with the same method).*/

app.Run();

What are the advantages of using Minimal APIs?

  • Enhanced Performance: Minimal APIs have a lower overhead compared to traditional MVC-based APIs, resulting in better performance and reduced resource consumption.
  • Easy to start, even for newcomers.
  • Reduce the ceremony to create a new API.

What are the Disadvantages of using Minimal APIs?

  • Low Code Quality*:* An example of that is the Dependency Injection (DI), as the injected instances will be in the method itself. The main point is that if we have a lot of injected instances the method shape will be a mess, which will lead to some confusion and it will be unreadable for the developers.
  • Compared to MVC, it may be harder to maintain as the project grows.

Conclusion

In today's fast-paced and efficient software engineering landscape, Minimal APIs exemplify the art of architectural finesse. By embracing a concise syntax and discarding outdated API development practices, developers have the opportunity to experience the benefits of reduced complexity, improved readability, faster iteration, and enhanced testing. As the field of software engineering continues to evolve, the prominence of Minimal APIs offers a valuable path toward creating effective, sustainable, and adaptable applications. The mantra of "less is more" calls upon us to tap into the untapped potential of modern software development.

Please refer to my GitHub Repo to find more examples of Minimal APIs.


Similar Articles