On 2023, Microsoft released the Minimal APIs, a powerful tool to create fast and light APIs with less dependencies from other libraries.
From official Microsoft documentation samples, we have the traditional Program.cs that contains all the business logic.
This is the sample I extracted from Microsoft, as you can notice, all logic is on Program.cs. That’s not a problem at all, but as your project increases this file can potentially become a mess.
So what Carter does?
Carters comes to make an extension of Minimal APIs and to make it more organized. It brings the concept of Modules instead of Controllers.
I’m going to create a project in order to explain it.
Once the project is ready, we need to install Carter.
Below are the definitions of some these classes.
The repository for Car entity.
So the base structure would be set like this:
The Modules works like Controllers. You don’t need to register Modules on Program.cs, because AddCarter() finds all classes that references to ICarterModule in the project get these registered.
Carter Modules
Like I said previously, Modules are like regular Controllers.
For this sample, I created a module called CarModule.
You can notice how similar with Minimal APIs, like I said at the beginning, Carter comes with the idea on extension for Minimal APIs.
Running the project
I’m going to show only a few endpoints from the project, if you are interested to see the full sample you can find it here: https://github.com/rpbs/CarterLibDemo.
POST
GET
Conclusion
If you want to expand a minimal Web API I recommend the usage of Carter. The Module pattern helps to set a organized tree instead of Program.cs including all business.