The Magical Pattern to Organize .NET Minimal APIs

  Рет қаралды 8,760

Gui Ferreira

Gui Ferreira

Күн бұрын

How to improve organization and maintainability of .NET Minimal APIs using REPR (Request-Endpoint-Response) Design Pattern.
💎 Be a Patreon to get the source code: / gsferreira
🚨 KEY LINKS
🤝 Support me on Patreon (and get access to source code) here: / gsferreira
👋 HEY FRIEND
If you're new to my Channel, my name is Guilherme, but you can call me Gui if Portuguese pronunciation is not your thing.
I see myself as a Minimalist Software Craftsman. That says a lot of what I talk about here.
So, why this KZbin channel? To share with you to simplify your life as a Developer through knowledge, technology, and practices.
If you are into those things as I do, don't forget to subscribe for new videos.
🔗 GET IN TOUCH
LinkedIn: / gferreira
Twitter: / gsferreira
GitHub: github.com/gsf...
Visit my blog: gsferreira.com
#dotnet #csharp

Пікірлер: 36
@oliviermalea9736
@oliviermalea9736 Жыл бұрын
Hey Gui, thx for the video ! Very efficient pattern, I used it in a project a few month ago with Ardalis endpoints and a blazor wasm front, it gave my team and I a strong and very fast capacity with quick results and obviously lots of clarity.
@gui.ferreira
@gui.ferreira Жыл бұрын
Amazing feedback Olivier! I'm curious: Did you move to it from MVC or it was a green field project?
@leozt99
@leozt99 Жыл бұрын
Indeed, Gui, you've given an interesting perspective! Similar to software architectures like monoliths and microservices, it's crucial to understand how to leverage each approach based on the problem at hand and the specific scenario. No solution is a silver bullet that fits all cases. Warm regards from Brazil!
@gui.ferreira
@gui.ferreira Жыл бұрын
Obrigado Leonardo! 100%. "It depends" it's a golden rule of software development.
@davidghydeable
@davidghydeable Жыл бұрын
I think that your handler methods should return Task. Then you can return Results.NotFound() and Results.Ok(result), etc
@Codewrinkles
@Codewrinkles Жыл бұрын
Very nice video. With Minimal API our ecosystem has arrived in the same place as the NodeJs one when it comes to oranizing your project. TBH, i don't think there will be a commonly agreed pattern of structuring minimal APIs in the near future. But what you have described here is a good starting point. However, when you start to build like this real production application you might find that you'll have some things that are common accross different "features". Like endpoint filters for example.Some filters might be global, others might be needed in 30% of your endpoints. Others in 90% of your endpoints. So how would you organize in this case? My working theory that seems to be backed up in practice is a hybrid organization both by features and conventions. In Angular this type of organization is very well documented and a de factor standard.
@gui.ferreira
@gui.ferreira Жыл бұрын
I agree with you. We are still uncovering ground. Maybe we will learn something from other languages and frameworks. Regarding your question. From my experience, often those "shared concerns" can either be grouped at a higher "bounded-context" level or they can be handled as an Infrastructure feature. A good example is Logging Middlewares.
@АнатолийБобко-щ6и
@АнатолийБобко-щ6и Жыл бұрын
Respect you for large scale of codespace!
@florimmaxhuni4718
@florimmaxhuni4718 Жыл бұрын
Im using similar to this approach except I have all in one file (endpoint, request, response and handler) because in the end this should be small (at least for now they are small and maybe if I see that I get bigger request, responses I guess I will change them to your structure)
@gui.ferreira
@gui.ferreira Жыл бұрын
I like small and focused files. However, I see the advantage of keeping them together. I think that the code will tell you once it's the moment to split things apart.
@bloggrammer
@bloggrammer Жыл бұрын
I built a framework similar to Carter framework which is what I'm using to organize my minimal api
@gui.ferreira
@gui.ferreira Жыл бұрын
Is it OSS?
@bloggrammer
@bloggrammer Жыл бұрын
@@gui.ferreira Nope. Maybe I'll consider making it an OSS later in the future. :)
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
When you want to reduce the number of folders and files, you can combine the Request, Endpoint, and Response types in a single file.
@gui.ferreira
@gui.ferreira 11 ай бұрын
We can even go to a Vertical Slice kind of approach, like I mention here: kzbin.info/www/bejne/mZLbhGpuZZuYqZIsi=4hNoqPs8WszEbPrh&t=480
@TerkelBrix
@TerkelBrix Жыл бұрын
Great video. Thanks. When building APIs sharing request-/response models can be powerful. Either in a shared project or a nuget packages. I really like the REPR-pattern and your approach. Is there a way to do both ? I am curious about your thoughts
@gui.ferreira
@gui.ferreira Жыл бұрын
Having a "contracts" package is an excellent idea. In particular inside an organization with many services. In that case, I recommend extracting the Request and Response object into the package but follow the same "Feature Folder" driven organization
@pinguincoder
@pinguincoder 3 ай бұрын
Hey, im using a similar pattern in my current project but I'm having problems using that endpoint in unit tests because the HandleAsync method needs to have all the dependencies which would normally be in the constructor. Do you have any advice for this?
@mdzieg
@mdzieg Жыл бұрын
Many folders instead of combining actions in one class. Minimal API and we end up with more classes than with the classic approach with controllers. But maybe I just have to get used to this...
@DieDona
@DieDona Жыл бұрын
You can approach it not as granularly as the video is. eg: you could just have a "ToDo" folder and have all endpoints in a single file. However i would keep requests and responses separated. just my 2c
@gui.ferreira
@gui.ferreira Жыл бұрын
The beauty of Minimal APIs resides in the freedom to arrange them as you prefer. You can keep a structure like a Controller, where each Action is an Endpoint. However, I prefer small and specific files and classes with a single responsibility.
@gui.ferreira
@gui.ferreira Жыл бұрын
@@DieDona 100%
@psybuck2002us
@psybuck2002us Жыл бұрын
With your endpoints segregated into multiple Endpoints.cs files, how would you effectively group endpoints if you needed to? Inheriting an abstract class for all the endpoint classes (if they were not static) with your endpoint group configurations could work, but would probably obfuscate your endpoint group configurations. How would you solve for that?
@gui.ferreira
@gui.ferreira Жыл бұрын
In that example I'm using static methods, however, I would possibly review that if the complexity demands it. Being pragmatic, in that case, likely I would look into Fast Endpoints if the complexity justifies it. I think this Fast Endpoints feature is what you are talking about: fast-endpoints.com/docs/configuration-settings#endpoint-configuration-groups
@davidghydeable
@davidghydeable Жыл бұрын
With all of these static methods how will you use dependency injection?
@davidghydeable
@davidghydeable Жыл бұрын
Oh! I understand. The dependencies are injected into handler methods instead of a constructor. So the class no longer needs to store dependencies that might only be used by a subset of operations. Nice!
@gui.ferreira
@gui.ferreira Жыл бұрын
That's it. You can find it here: learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-7.0#parameter-binding-with-dependency-injection
@qwertasd7
@qwertasd7 8 ай бұрын
though what if you dont want to use controllers (as isnt that the whole idea of minimal web api's, ea short code). The only problem which i face with minimal web api is that they can be hard to find under task main. if you endup having a lot of them.
@gui.ferreira
@gui.ferreira 8 ай бұрын
That's the point of the video. How to structure the minimal API so your API is maintainable and the "main" doesn't become a nightmare.
@mihaimyh
@mihaimyh Жыл бұрын
How about versioning?
@gui.ferreira
@gui.ferreira Жыл бұрын
Excellent question. In this approach, you first group things by domain concepts, however, regarding versioning it depends. I would group major versions separated at the root level if they are extremely different. If the API supports minor versioning, an option is to keep those together in the Endpoints.
@beemerrox
@beemerrox 10 ай бұрын
Been here, done that, moved on. Too many files & folders. Does not adhere to the world of micro services.
@gui.ferreira
@gui.ferreira 9 ай бұрын
Your comment is regarding MVC, right?
@cirusMEDIA
@cirusMEDIA 9 ай бұрын
We gain readability at the expense of way too many folders!!!
@gui.ferreira
@gui.ferreira 9 ай бұрын
I don't see that as a problem.
@angelobevilacqua6521
@angelobevilacqua6521 4 ай бұрын
Another example of making something simple complicated for nothing
Vertical Slice Architecture | The Best Architecture If…
11:51
Gui Ferreira
Рет қаралды 14 М.
Fix Your Controllers By Refactoring To Minimal APIs
14:56
Milan Jovanović
Рет қаралды 41 М.
Spongebob ate Michael Jackson 😱 #meme #spongebob #gmod
00:14
Mr. LoLo
Рет қаралды 11 МЛН
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 72 МЛН
Spongebob ate Patrick 😱 #meme #spongebob #gmod
00:15
Mr. LoLo
Рет қаралды 21 МЛН
Stop Using FirstOrDefault in .NET! | Code Cop #021
12:54
Nick Chapsas
Рет қаралды 79 М.
Should I Create A Minimal API Or Full API?
7:40
IAmTimCorey
Рет қаралды 37 М.
3 Implementations that will blow your mind 🤯
28:49
Next Increment
Рет қаралды 2,2 М.
How to Organize Your ASP.NET Program.cs File
15:40
Gui Ferreira
Рет қаралды 4,6 М.
Building better DTOs in C#
11:57
Gui Ferreira
Рет қаралды 6 М.
Forget Controllers and Minimal APIs in .NET!
14:07
Nick Chapsas
Рет қаралды 70 М.
Automatically Register Minimal APIs in ASP.NET Core | REPR Pattern
11:55
Milan Jovanović
Рет қаралды 14 М.
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 72 М.
The Free Way to Create Awesome PDFs in .NET
12:45
Nick Chapsas
Рет қаралды 49 М.
If Your Code Looks Like This... You're A GOOD Programmer
16:39
Continuous Delivery
Рет қаралды 72 М.
Spongebob ate Michael Jackson 😱 #meme #spongebob #gmod
00:14
Mr. LoLo
Рет қаралды 11 МЛН