Тёмный

Choosing between MVC Controllers, Minimal API, or FastEndpoints for C# APIs. 

Bald. Bearded. Builder.
Подписаться 5 тыс.
Просмотров 13 тыс.
50% 1

Опубликовано:

 

15 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 38   
@BaldBeardedBuilder
@BaldBeardedBuilder 6 месяцев назад
I've turned into a FastEndpoints superfan. What were your last few .NET API's written in?
@pinguincoder
@pinguincoder 5 месяцев назад
Minimal Apis for me so far. I never liked Controllers honestly because i want vertical structure of my app and controllers are all about horizontal structuring
@BaldBeardedBuilder
@BaldBeardedBuilder 5 месяцев назад
I get that. I loved controllers when they were first released, mainly because of the MVC pattern overall. I wasn't really building APIs at the time. But yeah, Minimal API would definitely be my choice for something quick and small, but I'm definitely fanboying over FastEndpoints right now for larger projects.
@mitar1283
@mitar1283 5 месяцев назад
​@@BaldBeardedBuilder Oh I have to try fast Endpoints then maybe I can make good use out of them in our upcoming company project
@BaldBeardedBuilder
@BaldBeardedBuilder 5 месяцев назад
@@mitar1283 Let me know how it goes and feel free to jump into our discord (bbb.dev/discord) with any questions.
@mitar1283
@mitar1283 5 месяцев назад
@@BaldBeardedBuilder Hey man I'm definetly going too. After all you always have to be open towards new things right?
@tbddevelops
@tbddevelops 6 месяцев назад
From a code organization standpoint, Controllers can often end up being "dumping grounds" in so much as we tend to align controllers with Entities in the DB, and developers will try and fit in. Ie. WidgetController "well, we need to get a report on the number of widgets sold, let's stick it in the WidgetController". And not to say you can't have the same situation appear with Fast Endpoints, but from the examples I've seen so far, endpoints are based more around the actions with verbiage (I won't go so far as to say restful), so even if a behavior does get misplaced, it's much easier to see and much easier to move.
@BaldBeardedBuilder
@BaldBeardedBuilder 6 месяцев назад
I agree. As in your example, I often see things like "ReportsController" that have hundreds of endpoints so that people feel better about not putting the method in your example in the WidgetController.
@mitar1283
@mitar1283 5 месяцев назад
Hey, I love the video, it gives a fast overview over the different options for creating web api endpoints. Also I didn't know about fast endpoints, thanks for explaining that ! However, I would love to touch on something that you didn't quite mention in your video. There is an attribute for the controller based approach that's called "FromServices" using that attribute you can inject dependencies into the individual methods instead of injecting them over the controllers constructor and having the mentioned downside of executing logic to fetch a certain service but not actually needing it. So that is not really an advantage of minimal APIs.
@BaldBeardedBuilder
@BaldBeardedBuilder 5 месяцев назад
Thanks for the feedback! It's is GREATLY appreciated. And you're absolutely right. You can apply the FromServices attribute to inject a service directly into a controllers method, but (and maybe this is just personal preference) seeing FromServices in controller methods always seemed like code smell to me. Do you use it frequently? Regardless, that's very valid input that I should have probably mentioned.
@mitar1283
@mitar1283 5 месяцев назад
@@BaldBeardedBuilder Hey man, first of All let me say one thing: After seeing your channel today for the first time I wasn't really sure if I should sub but now you have DEFINETLY gained yourself a new subscriber because you actually took the time to write such a detailed response (if we consider that you get quite a lot of comments) so THANK YOU for that ! Now to answer your question I actually encountered it the first time as a company guideline where I used to work but I've been using it ever since. Because to me it is quite the opposite the "FromServices" let's you know that this is not a Parameter That gets passed in the http request but that it get's loaded out of the DI Container. Additionally if you put the Parameters in the controller under each other so one line for each parameter you have the from services always at the beginning so you see which Parameters are from DI and also of course you should group them together so like all DI params at the end. Then I think it can even help with understanding what is going on at first glance and you get to keep all the Controller benefits that the other two don't have. But of course we are all different and we all have different preferences. But since I started with .Net 15 years ago (which was before the fluent syntax hype as you are surely aware) controllers do have a special place in my heart ❤️
@docpify
@docpify 4 месяца назад
I use Minimal API with static methods, gives me all the same benefits as controller classes, without some of the drawbacks You can in fact use comments with Minimal API, just map a static method rather than a lambda :)
@harrisonbs
@harrisonbs 5 месяцев назад
Minimal API doesn't expect the endpoints to be in one class. A good option is to move them into their own file for logical groups and use the group option. Use an interface with a register method to set them all up and then use an extension method to search the assembly for those Interfaces and call the registration method on each. Voila, potentially tens of files with hundreds of endpoints well structured and nicely registered.
@BaldBeardedBuilder
@BaldBeardedBuilder 5 месяцев назад
It's been a month since I recorded this. Did I say it "expects" them to be in one class? If so, I definitely misspoke. I agree with you and I group them with an extension method to register them, but oddly, that isn't what most Minimal API examples show. Maybe I should do a more in depth look at Minimal API with real-world code to discuss good project structure, better registration, etc. Regardless, thanks for joining in the conversation. Did you know we script, record, edit and discuss all these topics while we co-work together on Twitch? Check out bbb.dev/twitch and help us make better content while we work together.
@idan5323
@idan5323 Месяц назад
Exactly this.. what are the benefits of fastendpoints over this method?
@bitobrian
@bitobrian 6 месяцев назад
I love Fast Endpoints. I've used it on a few side projects over the last couple of years.
@BaldBeardedBuilder
@BaldBeardedBuilder 6 месяцев назад
Hard same. I've been SO impressed with it. Especially the included features that I don't have to find a Nuget packge for.
@reikooters
@reikooters 5 месяцев назад
Switched from MVC/Web API to Fast Endpoints 2 years ago and haven't gone back. It's improved a lot in that time too.
@JackBauerDev
@JackBauerDev 4 месяца назад
So if I put every controller endpoint in a separate class, I'd get the same perf as minimal API?
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Nice try. 😁
@blackpaw29
@blackpaw29 Месяц назад
Love the minimal api approach, I have a large minimal api in development, now I'm wondering if I should checkout FastEndpoints. How difficult would it be to migrate and existing MinimalAPI to FastEndpoints? Fortunately, maintaining client compatibility is not a requirement yet. Reservations: - Don't want to get locked into a 3rd party platform - Client Code generation, we autogenerate C# client libraries from the api swagger spec, can we do this with FP?
@svorskemattias
@svorskemattias 4 месяца назад
Wrong about dependency injection in controllers. You can inject them in each controller action.
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Thanks for the feedback! And you're absolutely right. You can apply the FromServices attribute to inject a service directly into a controllers method, but (and maybe this is just personal preference) seeing FromServices in controller methods always seemed like code smell to me. Do you use it frequently? Regardless, that's very valid input that I should have probably mentioned.
@svorskemattias
@svorskemattias 4 месяца назад
@@BaldBeardedBuilder Yes, it is actually my favorite approach. I don't see how it is any more smelly than injecting services into minimal api actions, and I don't even know whats so smelly about it. :) (Injecting dependencies by method parameters is the more functional way of doing it.) Yes, controllers lump together methods that don't relate too much too each other, but you should not look at controllers as objects in the object oriented sense. Look at them more like modules, a collection of functions more or less. With bigger minimal apis, you'd still want to organize your actions some way, and controllers already solve that problem, and bring a few bonuses with them (shared root route, openapi support etc). :)
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
That's interesting. In the end, it's likely personal preference. I think the "smelly" thing to me, and again, this is just my personal preference, is you don't normally learn to do injection with controllers via FromServices. It's an attempt to make controllers more functional, like Minimal API. While you CAN do it, it's not the normal pattern taught & used. Whereas in Minimal API it is THE pattern. Shared root routes are done in Minimal API with groups, but I 100% agree that OpenAPI support is much nicer with the Controller approach. Super good conversation though. You might want to join our discord at bbb.dev/discord where we talk about this nerdy stuff all the time. It's always great to learn & build together!
@MaxProgramming
@MaxProgramming 6 месяцев назад
Wow since when did you switch to dotnet? I really like the tech and C# but I'm so much in TS that I can't find a good usecase
@BaldBeardedBuilder
@BaldBeardedBuilder 6 месяцев назад
@MaxProgramming, I've been a .NET developer for 25ish years. 😁 But don't worry, I still love TypeScript, JavaScript and all the others.
@renynzea
@renynzea 4 месяца назад
Where I work we use MVC controllers and MediatR (along with fluent validator and other middleware). If I was to do another app I would switch to minimal API and MediatR, or checkout fast endpoints as it sounds very similar.
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Solid pattern. They all work well. And Fluent Assertions are great. I’ve got a video coming soon on them.
@snivels
@snivels 4 месяца назад
What font is that?
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Pretty sure that’s GitHubs new Monaspace font family. Specifically, the Krypton font in that family.
@snivels
@snivels 4 месяца назад
@@BaldBeardedBuilder Awesome, thank you mate!
@mubashir3
@mubashir3 4 месяца назад
Shocked face thumbnail? Does it still work? I thought we were quite past that phase. Maybe it’s time to try something new. Like stick your tongue out or pick nose booger to rope in viewers.
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Believe me, there is nothing more dehumanizing than taking those photos. However, after testing a dozen thumbnails, this still does best. I hate it. But hey, if you think the booger-picking would add value, I'm game. 🤣
@terjeber
@terjeber 4 месяца назад
The statement that minimal APIs must live in one class is patently false.
@BaldBeardedBuilder
@BaldBeardedBuilder 4 месяца назад
Okay, I could have worded that better. Perhaps saying they live in "a" class rather than "one" class, but the point was minimal API routes live in a class, where FastEndpoint routes are the class. Keep those nits coming. 😁
Далее
Should You Use a Class, Struct or Record in C#?
7:53
Forget Controllers and Minimal APIs in .NET!
14:07
Просмотров 68 тыс.
УГОСТИЛ БЕЛКУ МОРОЖЕНЫМ#cat #cats
00:14
Don't Make These Entity Framework Core Mistakes
8:48
.NET and C# are in trouble. Here is what I'd do.
10:57
The Magical Pattern to Organize .NET Minimal APIs
9:06
Don't throw exceptions in C#. Do this instead
18:13
Просмотров 258 тыс.
5 Rules For DTOs
17:56
Просмотров 41 тыс.
What is the Record Type in C#?
4:24
Просмотров 4,7 тыс.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Should I Create A Minimal API Or Full API?
7:40
Просмотров 36 тыс.
Don't Use Polly in .NET Directly. Use this instead!
14:58