Minimal APIs just got all I wanted in .NET 7

  Рет қаралды 62,997

Nick Chapsas

Nick Chapsas

2 жыл бұрын

Use code FILTERS for 20% off on my Minimal API course: dometrain.com/course/from-zer...
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I introduce you to the a new Minimal API feature of .NET 7 called Filters. Filters was the one thing I was personally missing since I moved from Controllers to Minimal APIs and I am glad the ASP.NET team finally added it.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet #dotnet7

Пікірлер: 184
@AlFasGD
@AlFasGD 2 жыл бұрын
7:05 you could also do `OfType().SingleOrDefault()`
@alexanders_trail
@alexanders_trail 2 жыл бұрын
Yes, but `SingleOrDefault` is more efficient without `OfType` , because OfType will try to cast all elements of sequence and return some IEnumerable object, and then you use only single element from this IEnumerable. Results of the simple benchmark with object[3] where only last element is of type: | Method | Mean | Error | StdDev | Gen 0 | Allocated | | OfTypeSingle | 124.15 ns | 2.422 ns | 2.790 ns | 0.0105 | 88 B | | SingleByType | 44.38 ns | 0.920 ns | 0.860 ns | 0.0038 | 32 B |
@AlFasGD
@AlFasGD 2 жыл бұрын
@@alexanders_trail That's strange; I would hope it worked more lazily, nice insight
@nanvlad
@nanvlad 2 жыл бұрын
@@AlFasGD for SingleOrDefault() it requires at least 2 matches, whereas FirstOrDefault() needs 1 match, so you would expect a better perfomance with OfType().FirstOrDefault()
@AlFasGD
@AlFasGD 2 жыл бұрын
@@nanvlad Nick used SingleOrDefault, that's why I used this here too
@nickchapsas
@nickchapsas 2 жыл бұрын
The problem is that FirstOrDefault is dangerous and SingleOrDefault is used intentionally. I want to throw if for some reason two object of the same type have snuck in there. This is a generic method which could be used everywhere so if some endpoint for some reason has two types of the same type I want this to be an exception. FirstOrDefault will hide the problem and cause a bug silently. It should be used VERY carefully.
@jingzheshan
@jingzheshan 2 жыл бұрын
That's super cool, I am waiting for this for a while now for MinimalAPI 👏
@jingzheshan
@jingzheshan Жыл бұрын
The name has been changed to IEndpointFilter now.
@AB-jt6ic
@AB-jt6ic 2 жыл бұрын
Another great video Nick!
@AthelstanEngland
@AthelstanEngland 11 ай бұрын
Excellent thanks.
@ggiiaaccoommoo
@ggiiaaccoommoo 2 жыл бұрын
1:56 Happy birthday. It's five days too late, but I guess it still counts.
@dksovfen
@dksovfen 2 жыл бұрын
I would like to see your view on IDisposable, best practices and when to use it where people might forget it. Love your videos 👌
@kaleidodeer
@kaleidodeer 2 жыл бұрын
I'm not sure there's much room to look at IDisposable - You want to use it to clean-up unmanaged resources explicitly. Sure, you could wait for the GC to get around to cleaning up the object to call the destructor/finalizer that has your code to clean it up implicitly, but you have no idea when thats going to happen. IDisposable lets you clean up the unmanaged resources immediately leaving only the wrapper on the managed side that the GC has to clean up. (You can disable finalization calls on a Dispose call so the GC won't try to repeat it) You can also use a Using statement to call Dispose implicitly at the end of the scope which is handy for filestreams. Only other instance I have seen for this is relating to events. If a class for example is subscribed to a publisher/event then it will be kept alive/won't be collected until it unsubscribes or until the publisher is cleaned up. Publishers keep subscribers alive, but subscribers don't keep publishers alive. Some people opt for a weak pattern to prevent this.
@ME-dg5np
@ME-dg5np Жыл бұрын
Fantastic filter validators ! ☀️🕺🌲
@Conner112
@Conner112 2 жыл бұрын
Nice, this is cool. We've been running validation through a mediatr pipeline then handling the validation exception in middleware to return bad requests. It'll make more sense to short circuit all that overhead and move validation entirely to the API.
@nickchapsas
@nickchapsas 2 жыл бұрын
Yeah i like MediatR to only handle my domain level validation and have my api level validation on the contract itself as a filter so that's a perfect addition
@zubairahmed3877
@zubairahmed3877 2 жыл бұрын
@@nickchapsas can you please give an example of domain vs contract level validation. i want to understand the need for both, seems most validation will be domain level even simple email address could be considered domain level.
@nickchapsas
@nickchapsas 2 жыл бұрын
@@zubairahmed3877 For example whether a property is mandatory or not or whether an email looks like an email. That would be api contract validation. Domain validation would check things specific to our application's domain logic, for example if another person with that same email is allowed in the system or not.
@benjaminb.4043
@benjaminb.4043 2 жыл бұрын
@@nickchapsas Does your domain layer generally check the format of the email too? If yes, wouldn't this result in duplicate validation code? I generally never validate on api side as the domain already has rules about data format when creating Value Objects. Is that a bad approach?
@nickchapsas
@nickchapsas 2 жыл бұрын
@@benjaminb.4043 I go back and forth on email validation. Sometimes it’s in domain only sometimes it’s in both places but that’s a bit of a problem so I tend to only keep it on the domain
@Dimonina
@Dimonina 2 жыл бұрын
So, what we see, is that the webapi approaches are turning more to a functional style of programming. Mediatr is a kind of a wrapper of a function, now these minimal apis and filters, we just combine functions together. Every release we have less OOP and more functional things. I hope some day we'll have an ability to use decriminated unions in C# and this will allow to perfectly describe business logic of the app.
@nickchapsas
@nickchapsas 2 жыл бұрын
It makes sense since even on the C# level they are getting heavily inspired by F# features and functional concepts in general
@Dimich1993
@Dimich1993 2 жыл бұрын
Happy B-Day man!
@Yous0147
@Yous0147 2 жыл бұрын
This is really cool, I'm glad that's a possibility in 7. So the reason why this is not possible earlier, is it because the AddFilter method isn't defined?
@sohampatel1063
@sohampatel1063 2 жыл бұрын
Now that Parameters property has been renamed to Arguments.
@UweKeim
@UweKeim 2 жыл бұрын
Happy birthday!
@BK-19
@BK-19 2 жыл бұрын
It gose foo foooo.. nice :):):)
@maozaorox
@maozaorox 2 жыл бұрын
Great video again Nick. Just in case, there is any performance difference between "Classic API" using Controllers and Minimal API?
@nickchapsas
@nickchapsas 2 жыл бұрын
There is yeah. From 5-25% depending on how efficient your IO calls are. If you have a database call that takes 100ms, then you won’t see the difference since the db call will “cover” it
@kelton5020
@kelton5020 2 жыл бұрын
Filters seem nice. I think pulling the validations from the assembly using reflection is evil voodoo and should be avoided. A clearer pattern would be too map the model validator to the validator filter directly like ValidationFilter where T : IValidator (or whatever the validator interface is) so you could call .AddFilter()
@nickchapsas
@nickchapsas 2 жыл бұрын
I don't agree that assembly scanning is bad. It is extremely common and the .NET platform is using it extensively behind the scenes and it's something that almost all libraries, like MediatR, AutoMapper, FluentValidation etc have adopted.
@kelton5020
@kelton5020 2 жыл бұрын
@@nickchapsas I know what you mean. I used to write a lot of code like that because it's more convenient and less typing. I've found over the years though that it ends up causing more headaches than it's worth. When it's in the .net framework and the usage is well documented and replicated and explained in tons of tutorials/codebases it's a little bit different. If you're writing code like that in a team, you're essentially adding magic behaviour and it causes issues and confusion. As an example, being able to right click and Find All Usages of a class/interface/method (or a text search) to see where it's being used is invaluable when debugging or refactoring. I can't tell you how often this pattern has burnt all of the different teams I've worked on, but I can say every time it gets implemented it ends up being way more inconvenient in the long run by far. Technical debt at its finest 💸.
@cchance
@cchance Жыл бұрын
@@kelton5020 IT sounds more like poor documentation and commenting is more your issue than magic behaviour. The fact that most large projects and the framework itself can use it means that it's not the issue. In fact the fact that large ... GIANT projects use it so heavily and work find with MANY MANY developers touching it points toward the issue being elsewhere on those teams you mentioned. Like i said most likely in a lack of documentation.
@DanteDeRuwe
@DanteDeRuwe 2 жыл бұрын
Another way Minimal API's comes closer to having certain features of Mediatr (in this case this looks a lot like mediatr pipelines). Nice vid; very interesting!
@davidmataviejo3313
@davidmataviejo3313 2 жыл бұрын
Filters and Middleware are old feature in .net core. You don't need to use mediatr or .net 7
@F2H16
@F2H16 Жыл бұрын
Hi, it seems [FromForm] is not available to use in minimal APIs. I needed to post an object with files (.text/.pdf) from a frontend (React) application to a .NET core 6 minimal API which does not seem to be possible using minimal API. Only because of that I might need to move back to the controllers which is really a pity. Do you see a workaround?
@gerardoserrano7886
@gerardoserrano7886 Жыл бұрын
Hi, do you have the repo updated with filters in .net 7 with minimal APIs?
@ilanb
@ilanb 2 жыл бұрын
Super cool! Solves the problem of deserializing the request multiple times. Does this work for other content types as well, such as forms?
@nickchapsas
@nickchapsas 2 жыл бұрын
As long as they get mapped to the object then yeah
@djstypen
@djstypen 2 жыл бұрын
Hi Nick. I have a new project to develop and planning to use minimal API's approach after following your recent videos. Would you recomend to use FastEndpoints library or do you think its better to organize endpoints on your own? Thank you for sharing such great content!
@nickchapsas
@nickchapsas 2 жыл бұрын
Totally. That’s what I do as well
@danijelzg001
@danijelzg001 2 жыл бұрын
@Nick can you make video about nullable and how does it affect performance
@EmersonBottero
@EmersonBottero 2 жыл бұрын
Awesome!!! 🤓 Questions: 1 - Do I even need MediatR when I have IRouteHandlerFilter and reflection working together? 2 - How can I create an endpoint with the validations for each Commands/Queries, so I can use as schemas to generate forms in any frontend framework? (Why this is not a thing I don't know... I guess everyone replicate their validations if not using a C# compatible language like blazor ou MAUI)
@nickchapsas
@nickchapsas 2 жыл бұрын
1. in my opinion, no 2. I didn’t understand this one
@DemoBytom
@DemoBytom 2 жыл бұрын
One thing I don't like here, is that you specify the validation filter in a different place, to the expected parameters of the endpoint. If at any point the input model of the endpoint changes, you will only know about it in runtime. It's probably solvable with some custom Roslyn analyzers, or API automatic tests tho, but I'd still love things like validation filters being setup on the endpoint method, where you have fuller context. Maybe an attribute and assembly scan adding filters at the API startup?
@nickchapsas
@nickchapsas 2 жыл бұрын
You won't only know during runtime. I would expect people to have unit or integration tests around this. It would be very bad if you didn't have them, even if it was a generic constraint driven by the request type.
@bryanlewis3643
@bryanlewis3643 2 жыл бұрын
This is great. You never posted a followup to the FastEndpoints OS project video you did a few weeks back (you said in that video that you were going to do a followup). We are trying to determine if we want to go with pure Minimal APIs or try Fast Endpoints for our next production API. We're curious to know your further thoughts.
@nickchapsas
@nickchapsas 2 жыл бұрын
I sent that video privately to the creator of FastEndpoints and he addressed all the issues. I didn’t want to put the library on a negative light.
@cchance
@cchance Жыл бұрын
How would you tie this in with the minimalist mediatr you demo'd
@oleggolovkov957
@oleggolovkov957 2 жыл бұрын
I think there's `OfType` LINQ extension method so that you don't have to do that `x = > x.GetType() == typeof(T)`
@nickchapsas
@nickchapsas 2 жыл бұрын
OfType will try to cast every type. The expression will only check it
@gunnarliljas8459
@gunnarliljas8459 2 жыл бұрын
@@nickchapsas It will only cast types that passes an "is" test, which would be more flexible than exact type matching.
@rogeriobarretto
@rogeriobarretto 2 жыл бұрын
That’s quite a good point. x => x is T would be slower?
@gunnarliljas8459
@gunnarliljas8459 2 жыл бұрын
@@rogeriobarretto It would be slower, but probably not in any significant way.
@UweKeim
@UweKeim 2 жыл бұрын
Making "Minimal API" complex enough, one step a time, until it is not minimal anymore.
@nickchapsas
@nickchapsas 2 жыл бұрын
Minimal API is about choice of structure and simplicity of building blocks. Not about having everything in a single class.
@rigardtsmit1816
@rigardtsmit1816 2 жыл бұрын
My thoughts exactly!
@old_penguin
@old_penguin 2 жыл бұрын
In .NET 10 it will be renamed to Maximal API
@thethreeheadedmonkey
@thethreeheadedmonkey 2 жыл бұрын
It enables more functional programming, which is usually quite minimal (and correct) :-) Moving away from the rather clunky and OOP + convention based setup code is an improvement that will reduce time-to-production for a lot of APIs.
@lawrencejob
@lawrencejob 2 жыл бұрын
Totally agree with this - this isn’t where I was hoping the API would go. I’m just relieved it’s not attribute based at least!
@meny054
@meny054 2 жыл бұрын
Thank you Nick for all the great content. I getting the idea of the difference between MVC and minimal API, and understand that the minimal API is lighter. Still I more like the MVP approach with functions and property decoration instead of declaring evrything in the code itself Is more automated for my opinion The question is can it be done with the new approach? Or that this way is not recomended anymore?
@MrBa143
@MrBa143 2 жыл бұрын
The purpose of Minimal API is to be minimal. All the help and automation you get in a MVC project is what causes the poor performance. If you start introducing those helpers into a mininal api, then you might aswell just use MVC.
@cchance
@cchance Жыл бұрын
The issue is MinimalAPI is for... APIs MVC is for things that need ... Views... Hence mVc api's don't need views.
@AthelstanEngland
@AthelstanEngland 11 ай бұрын
Nick has a video on structuring Min APIs for larger projects which may be worth watching, called in Defence of Minimal APIS or similar.
@Aldecode
@Aldecode 2 жыл бұрын
Can we just register validators from the assembly in Startup, and trigger them automatically before stepping into the endpoint?
@jfpinero
@jfpinero 2 жыл бұрын
Yes you can, been doing it for years now (using fluentvalidation)
@ForrestBrown
@ForrestBrown 2 жыл бұрын
Combining FluentValidation with controllers, yes, but with minimal apis the recommended approach (for .net6 anyway) is to use IValidator directly.
@pdevito
@pdevito 2 жыл бұрын
Swagger documentation capabilities were also a gap last I checked… I think all the other major boxes are checked after that?
@nickchapsas
@nickchapsas 2 жыл бұрын
Swagger documentation was supported but they are expanding on it even further in .NET 7. Which feature were you missing?
@happytrackvideo
@happytrackvideo Жыл бұрын
Is there any performance test if minimal api run faster than api controller? It looks like a lot of work to organize the minimal api.
@lawrencejob
@lawrencejob 2 жыл бұрын
Someone will invent a architecture template that puts the entire app business logic using just filters and we’re all going to be made to use it by our architects
@bjuraga
@bjuraga 2 жыл бұрын
dont give ideas :)
@mome3807
@mome3807 2 жыл бұрын
could you even go further and use source generator to create the specific customer type at compile time instead of reflection GetType ?
@nickchapsas
@nickchapsas 2 жыл бұрын
No because source generators can’t change existing code but the get type check is extremely fast and not a problem
@rajon69
@rajon69 Жыл бұрын
I can't find IRouteHandlerFilter, my project is .net 7, but getting The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)
@honguyenminh
@honguyenminh 2 жыл бұрын
Basically middleware, awesome!
@rafaelgonzales585
@rafaelgonzales585 2 жыл бұрын
Hi Nick, the coupon is not working anymore, any chance to get one?
@frankoppermann1877
@frankoppermann1877 Жыл бұрын
I don't find the "AddFilter" Method and the IRouteHandlerFilter Interface. My dotnet --version is 7.0.100. (Downloaded today). Any idea?
@marcincholewka9646
@marcincholewka9646 Жыл бұрын
I have the same problem and almost exact the same SDK version (7.0.101). Does anyone know the answer? I use Rider IDE. @edit: Okay I have the answer. It has been renamed to "IEndpointFilter" interface. Similarly, "AddRouteHandlerFilter" method has been replaced with "AddEndpointFilter" etc.
@luisyagual115
@luisyagual115 Жыл бұрын
IRouteHandlerFilter is renamed to IEndpointFilter, and AddFilter is renamed to AddEndpointFilter.
@RemX405
@RemX405 2 жыл бұрын
So basically a middleware that you can scope to specific endpoints?
@nickchapsas
@nickchapsas 2 жыл бұрын
Effectively yes
@DoreHdx
@DoreHdx 2 жыл бұрын
So is there a fancy way of doing this in "normal" controllers?. I am aware of Attributes and the Middleware approach but those seem a bit more "hacky".
@steve-ardalis-smith
@steve-ardalis-smith 2 жыл бұрын
A big benefit of filters in MVC is that you can apply them per-controller or per-project. Do you know if that's available (yet) with this approach, or do you literally have to go and add `.Filter()` to every endpoint to do fully cross-cutting concerns (like model validation for instance)?
@senor_m6673
@senor_m6673 2 жыл бұрын
i have never worked with minimal apis so i dont know if there is a framwork way to do project level filters, but a c# way that comes to mind would be to just write an extension method on the webapplication, so you basically have your own mappost where you already add the filters if you know what i mean
@nickchapsas
@nickchapsas 2 жыл бұрын
You can apply filters per endpoint or per project. That’s exactly what I showcase in the video
@senor_m6673
@senor_m6673 2 жыл бұрын
@@nickchapsas yeah the per endpoint are clear but i couldnt find a per project one in this video. maybe im misunderstanding something but all filters shown in the video look like enpoint scoped to me
@steve-ardalis-smith
@steve-ardalis-smith 2 жыл бұрын
@@nickchapsas Sorry I must have missed the per-project one.
@DanielMauritzson
@DanielMauritzson 2 жыл бұрын
@@nickchapsas I might be missing something but I'm only seeing AddFilter method used on MapGet, MapPost etc in the video, meaning I would need to call that for all endpoints using the filter. Is there no way to add a generic validation filter for all endpoints? If not that is still a good use case for MediatR since it is possible to do that there.
@DigitalNomadOnFIRE
@DigitalNomadOnFIRE Жыл бұрын
Which IDE are you using here?
@nickchapsas
@nickchapsas Жыл бұрын
JetBrains Rider
@fl028
@fl028 2 жыл бұрын
Does the minimal Api Template has Swagger build in?
@nickchapsas
@nickchapsas 2 жыл бұрын
It does
@kaanacar8340
@kaanacar8340 10 ай бұрын
Why IRouteHandlerFilter interface not found .net 7
@bartzalewski
@bartzalewski 2 жыл бұрын
Are you updating the minimal api zero to hero course with a filters section by any chance?
@nickchapsas
@nickchapsas 2 жыл бұрын
When .NET 7 comes out I will
@unshiftbit7680
@unshiftbit7680 2 жыл бұрын
So in short, filters are middleware or uses the middleware pattern. Nice, we getting closer to express js
@doctor9101
@doctor9101 2 жыл бұрын
Looks like we wait for another 6 months , and Visual Studio will support all these features
@Martin-kj1od
@Martin-kj1od 2 жыл бұрын
This is great. I have a question. In my apps i use controllers with mediator. With minimal api does it make sence to use mediator ? Or is it better to separete apis to more files to avoid boilerplate that comes with mediator?
@nickchapsas
@nickchapsas 2 жыл бұрын
It doesn’t really make sense no. You can get an example of the structure here github.com/Elfocrash/clean-minimal-api
@rogeriobarretto
@rogeriobarretto 2 жыл бұрын
Is there a way to setup a filter to all the endpoints at once without needing to be endpoint specific?
@nickchapsas
@nickchapsas 2 жыл бұрын
You can iterate over the endpoints and call the AddFilter method
@lolroflxd
@lolroflxd 2 жыл бұрын
@@nickchapsas how to iterate over all endpoints?
@nickchapsas
@nickchapsas 2 жыл бұрын
@@lolroflxd You can store the returned value of the registration in a list and then iterate the list
@w4.k
@w4.k 2 жыл бұрын
0:53 wait do you mean you use it professionally in production or only on your own projects?
@nickchapsas
@nickchapsas 2 жыл бұрын
Both
@RaWMajkeL
@RaWMajkeL 2 жыл бұрын
It works only in dotnet 7 preview?
@nickchapsas
@nickchapsas 2 жыл бұрын
For now yes
@moitoius
@moitoius 2 жыл бұрын
By the way your invalid email is actually a valid email address :). SMTP did send directly to a host only in its first iterations, as that's how arpanet operated. The mailbox portion was added later, and the most recent version of the spec still allows for hostname-only addresses. The only correct way to validate an email address is to try and send an email to it.
@RogerValor
@RogerValor 2 жыл бұрын
Tell that to amazon, who still not allow any email with a TLD from after 2014 to be added as partner email. A bug I try to report every 6 months. Email validation is an artform with many distinct tastes.
@funknick
@funknick 2 жыл бұрын
I know there are a lot of REST API's still out there and I think the recent changes to make API building more modular in C# is great. Moving away from the dated Controller architecture is really nice to see. I wonder if they're sort of still playing last years game though. REST API's are becoming more and more antiquated for me as most of the companies I and others work for are heavily investing in GraphQL API's now. In this GraphQL world, REST is nearly non-existent and used for hardly anything more than defining the `api/graphql` endpoint on a server. I hear folks saying to "check the future roadmap for minimal APIs." Is there something down that road which will compete with the strongly typed API schema, type handling, validation, auth flow management, etc that GraphQL offers?
@JustinAdler
@JustinAdler 2 жыл бұрын
Honestly, I think you're missing the point or misunderstanding the role of GraphQL. GQL is a way to aggregate various backend API's for various frontends - think Desktop (browsers) show different data to a mobile phone version (think: UI screensizes can determine what to show, etc). A GQL still needs to -source- the data from somewhere and that's where backend API's come in handy. Especially if the API's are various microservices.
@funknick
@funknick 2 жыл бұрын
@@JustinAdler That's not how we and many of my clients have our GraphQL API's built. It's also not what Apollo, "the Guild" of GraphQL, or the GraphQL open source project maintainers recommend. We do not do the "sourcing" that you're referring to as it's inefficient performance wise compared to implementing native GraphQL resolvers. GraphQL is very much a replacement to REST API's. The sourcing you're talking about is GraphQL Federation to stitch multiple API's together. I agree that this is a thing that is done, but on greenfield projects we and many others I know forego the entire REST -> GraphQL layer in favour of just straight up implementing the resolvers directly interfaced to the GraphQL schema without any REST data endpoints at all. Further on the topic of stitching, federation, and providing an API for multiple front-ends, it works better if the resolver types are directly interfaced into the GraphQL schema because it removes the mapping layer from your REST API to the types the resolvers provide. Also, by using the resolvers instead of a REST -> resolver mapping, you gain inherent bonuses for caching via DataLoader's and hierarchical resolution of data with incredible flexibility to retrieve only exactly what you ask for.
@KishorTiwari
@KishorTiwari 2 жыл бұрын
@@funknick Agree
@AthelstanEngland
@AthelstanEngland 11 ай бұрын
@@funknick seems like a big learning curve for GraphQL though. RESTApi, especially this minimal approach much easier to understand... for me anyway!! Plus I inherently want to stay away from anything related to Facebook!! :)
@funknick
@funknick 11 ай бұрын
@@AthelstanEngland you are absolutely correct. It is a decently sized learning curve. I came from REST based web apps and had to jump into GraphQL for my current job. It took me a year to really get comfortable with the API pattern. I only push GraphQL because every company I know seems to be implementing with it or migrating to it (for better or worse). Ignoring industry trends is a good way to pass up relatively easy money. Who doesn't want easy money? For many projects, GraphQL is definitely overkill. Ultimately, it's not even necessary. GraphQL is to REST what React is to Angular. The reality is, no one really needed a replacement for REST or Angular, it already worked fine... but this is web dev, where we love to reinvent the wheel and call it a "revolution" every decade. *shrug* I get paid to do GraphQL, so I do GraphQL. Even if I'd prefer to just stick to my tried and true patterns and tools, I'm repeatedly forced to learn new things because someone saw a "shiny" and chased after it. There's no use fighting the tide, surf it and make the money. Fighting it will just tire you out and make life more difficult. Thankfully, in the case of GraphQL, the "shiny" isn't a random bawble, it's a decently cut gemstone.
@Max1weber
@Max1weber 2 жыл бұрын
is the course not in the Bundle ?
@nickchapsas
@nickchapsas 2 жыл бұрын
It is not. Course bundles are logical grouping and that course doesn't belong in one yet
@kblyr
@kblyr 2 жыл бұрын
When building a REST API in .NET, will you go with Minimal API or Controller-based?
@nickchapsas
@nickchapsas 2 жыл бұрын
I use Minimal APIs
@gab8169
@gab8169 2 жыл бұрын
i feel like controllers produce more readable code, but each to their own
@lolyasuo1235
@lolyasuo1235 2 жыл бұрын
I would use controllers because of features but if i had special case where i wanted those 2-3k extra RPMs, i would go minimal. If adding more instances was not an option.
@degrauxmaxence8871
@degrauxmaxence8871 2 жыл бұрын
Hello, sorry i'm a bit new to web api, but it's look great features. Is this applicable to MVC approach ?
@nickchapsas
@nickchapsas 2 жыл бұрын
This is only applicable to minimal APIs. MVC has a version of this already
@pilotboba
@pilotboba 2 жыл бұрын
With controllers, you can attribute with ApiController and it will automatically run validations and return error responses if there is a validation failure. you can use the fluentvalidations.mvc (I think it is) nuget package to plug into this without writing the filter yourself.
@nickchapsas
@nickchapsas 2 жыл бұрын
@@pilotboba Yeah but do you know how much stuff you are actually adding behind the scenes because of that ApiController attribute? You don't just get the validation. You also have no control over how that's implemented. If you want to optimize or customize anything you are extremely limited
@pilotboba
@pilotboba 2 жыл бұрын
@@nickchapsas sure.. You are required to use attribute routing. You get automatic 400 responses You get binding source parameter inference. Multipart/form-data request inference for FromForm attributes. You can also customize it with ApiBehaviorOptions. It's not very heavy and pretty much what you want most of the time. And it is opt in. You should always know what the magic gives you.
@mze745
@mze745 2 жыл бұрын
It feels like they slowly want to replace MVC (or create a competitor) with Pipeline RequestDelegates.
@nickchapsas
@nickchapsas 2 жыл бұрын
Which is great because you can build exactly what you need without the limitations from MVC
@Tsunami14
@Tsunami14 2 жыл бұрын
I remember Nick saying the min api's don't allow for properly isolates unit tests. Is that no longer true?
@nickchapsas
@nickchapsas 2 жыл бұрын
They are fixing that too in .NET 7
@jackkendall6420
@jackkendall6420 2 жыл бұрын
@@nickchapsas I would be interested in seeing more about this
@nickchapsas
@nickchapsas 2 жыл бұрын
@@jackkendall6420 I will be making a video on it
@denishpatel6038
@denishpatel6038 2 жыл бұрын
Hello Nick, it would be great if you can provide code repo so we can try it easily. Thanks!
@nickchapsas
@nickchapsas 2 жыл бұрын
The code in my videos is available to my Patreons
@simply3065
@simply3065 2 жыл бұрын
The MinimalAPI just got a bit more powerful :)
@SayWhaaaaaaaaaaaaaaaaaaaaaaat
@SayWhaaaaaaaaaaaaaaaaaaaaaaat 2 жыл бұрын
When to develope when new versions and features comes out every 2 days?!! :D
@zzing
@zzing 2 жыл бұрын
“Oops! An error occurred! Please refresh the browser” appears after a purchase course button on your website on iPhone.
@grapes639
@grapes639 2 жыл бұрын
Looking at your code here, how is this simpler than current controllers? Looks to be just as much code, if not more, with the fact that you have that "MapCustomersEndPoints" method, and then still have to actually create the actions just like before. What am I missing here?
@nickchapsas
@nickchapsas 2 жыл бұрын
Controllers are a flawed/bad construct to use for APIs to begin with. They make sense architecturly and the only reason why we use them was because they just happened to have been there in MVC. Now for the video, that’s just one of the structures. My personal favourite way to structure an api with minimal api technology is this: github.com/Elfocrash/clean-minimal-api
@grapes639
@grapes639 2 жыл бұрын
@@nickchapsas Thank you for for the reference. I'll have a look.
@codeme8016
@codeme8016 Жыл бұрын
I feel in order to skip every single simple Controller I have to add a lot of extra codes/files in order to achieve the same thing. What's the point of using Minimal API now? Am I missing something?
@antonmartyniuk
@antonmartyniuk 2 жыл бұрын
While I like minimal APIs I don't like some of the missing features. In Controller based approach you only register Fluent Validators from an assembly and without adding a single line of code you have a fully working validation at Controller level. And in the new .NET 7 you need to have at least a custom filter. That sucks
@nickchapsas
@nickchapsas 2 жыл бұрын
You do add code in the startup to wire the validators and override the built in validation. You can get a similar thing with FastEndpoints which just need a validated to exist to register. Here is an example github.com/Elfocrash/clean-minimal-api
@5ROC
@5ROC Жыл бұрын
FILTERS code is not working. Are talking about auth Tokens in the course ?
@chopzdcat
@chopzdcat Жыл бұрын
Yep, FILTERS code not working. I'll wait until it works. :)
@ztBlackGad
@ztBlackGad 2 жыл бұрын
:) in several iterations it will be same mvc pipeline) or mediatr behaviour.
@nickchapsas
@nickchapsas 2 жыл бұрын
The difference is that you choose to opt in to the features you need on certain scenarios. It doesn't matter if eventually they have the same capabilities as Controllers. That was never the problem they are solving anyway. What matters is that the ground zero stays completely stripped off of everything,
@davidfowl
@davidfowl 2 жыл бұрын
@@nickchapsas 100% accurate
@ztBlackGad
@ztBlackGad 2 жыл бұрын
@@nickchapsas don't get me wrong :) I started from owin pipelines so it was obvious feature for me. Just funny how different framework transforms to generally used patterns. Waiting for another silver bullet framework based on this )
@bjuraga
@bjuraga 2 жыл бұрын
So, if you ever need to expose the customer service over a different protocol or even simply expose it via a CLI, you would have to either write the exact same validation per framework, or inject it before the service. I wonder why not build it in the service itself. Said in another way, why would you allow your service to save an invalid customer?
@semen083
@semen083 2 жыл бұрын
But it is the same thing, that ordinary middleware can do in the early version of minimal api.
@nickchapsas
@nickchapsas 2 жыл бұрын
Middleware applies to all endpoint no matter what. Filters are endpoint specific
@kawthooleidevelopers
@kawthooleidevelopers Жыл бұрын
I was coding along only to realise later that I'm on .Net 6. Silly me.
@sergeybenzenko6629
@sergeybenzenko6629 2 жыл бұрын
So you mean you couldn't do it before .NET 7? What was missing? I thought custom middleware was there since .NET Core 1. Probably it's more complicated because of different routing mechanizm.
@nickchapsas
@nickchapsas 2 жыл бұрын
Correct, you couldn't. In Minimal APIs you could not have filters or action specific middleware. Now, in .NET 7, you can.
@davidfowl
@davidfowl 2 жыл бұрын
@@nickchapsas Right, the main different is the context. Access to arguments before the method is called and the ability to change the result before returning.
@minecraftermad
@minecraftermad 2 жыл бұрын
Honestly i don't like it because the execution order isn't from up to down....
@BGraves
@BGraves 2 жыл бұрын
You born 4/20? I was
@federicodibernardo2719
@federicodibernardo2719 2 жыл бұрын
why dont use .EmailAddress() as validator instead of regexp? :)
@nickchapsas
@nickchapsas 2 жыл бұрын
Very good point. My brain completely forgot about that
@OlegKosmakov
@OlegKosmakov 2 жыл бұрын
The default EmailAddress validator (so-called asp-net compatible) only checks that email address has '@' sign in it, and it is not first or last character. The regex check is implemented as alternative check mode, but is marked obsolete for some reason and not recommended.
@nickchapsas
@nickchapsas 2 жыл бұрын
@@OlegKosmakov I have to look deeper into this then because I remember someone using regex instead of the EmailAddress one at work
@pilotboba
@pilotboba 2 жыл бұрын
@@OlegKosmakov Because the RFC for a valid email address is insanely complicated. Also not all email providers/services/software supports what is otherwise valid. The best way to go is to use the simple contains an @ sign, and send an email verification email with a URL validate link token thing. :)
@qwer1234cvb
@qwer1234cvb 2 жыл бұрын
Nick, what do you think about the necessity to handle 404 error separately from othe validation logic? I would love to be able to move this part from controllers/endpoints to validators but keeping the ability to return the NotFound response when an entity does not exist. Unfortunately, FluentValidation does not provide a way to do so. That is why I wrote my own extension for this: Pankraty.FluentValidation.HttpExtensions. Please, check this out, I think, you may find this approach very clean and convenient.
@nickchapsas
@nickchapsas 2 жыл бұрын
I don't like that type of response logic contained in my validator. My API-level validators should have one job. To tell me if the request coming in is valid or not. The 404 when a resource is not found is driven by my handler logic because it is a REST concern. I don't like handing that responsibility to FV
Write cleaner APIs in .NET 7 with MediatR
15:25
Nick Chapsas
Рет қаралды 59 М.
Goodbye controllers, hello Minimal APIs - Nick Chapsas - NDC London 2022
52:02
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 33 МЛН
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 99 МЛН
Alat Seru Penolong untuk Mimpi Indah Bayi!
00:31
Let's GLOW! Indonesian
Рет қаралды 15 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
The Magical Pattern to Organize .NET Minimal APIs
9:06
Gui Ferreira
Рет қаралды 8 М.
The Blazor Competitor is Here!
15:08
Nick Chapsas
Рет қаралды 65 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
.NET 7 Minimal API Route Groups Are INSANE!
0:45
Codewrinkles
Рет қаралды 16 М.
Generative AI into ANY .NET App with SemanticKernel
12:39
Nick Chapsas
Рет қаралды 41 М.
The New .NET 9 HybridCache That You Must Upgrade To!
14:34
Nick Chapsas
Рет қаралды 47 М.
Getting Started with Event Sourcing in .NET
37:07
Nick Chapsas
Рет қаралды 50 М.
NativeAOT for .NET APIs Is Here and It’s INSANE!
14:07
Nick Chapsas
Рет қаралды 86 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 252 М.
"I Lost a Job Because of This Codebase"
14:08
Nick Chapsas
Рет қаралды 60 М.
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 33 МЛН