Use The New Way To Build Fast .NET REST APIs With Minimal Code And Configuration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you start using controllers let me tell you that there is a better way to build your networks apis which is simpler more lightweight and much more focused in this video I'm going to show you how to move front additional controllers into my new favorite way of building downloads apis which is known as minimal apis let's Dive In so here you have a fairly standard.networks API which is currently built using controllers so as you can see we do have our games controller over here and it's only meant to manage a small list of games and so as you can see it does have the standard method to go ahead and get all of the games and then to retrieve one specific game by ID another method to create a game be a post and then I met to update a game via a put verb and then finally our method tool delete the game as you can see this is all using a standard repository implementation to actually store those games in memory in this case and then this is what we want to go ahead and move into the minimal apis approach and so the first thing that we want to do in order to move to minimal apis is actually go back into our permanent CS file over here and this is where we want to start defining those embos you don't have to specify a brand new class for manual apis also you may end up refactoring your code into an actual class just to group them together but you don't have to do it right so what you want to do here is let me scroll down a little bit over here unless had a line just after a map controllers so we can start by defining our get a all endpoint so to do that let's do just app that map get right so this defines a get type of endpoint and then the first thing to specify here well we know the pattern uh the pattern is going to be that pad that's going to be followed by the client in order to reach this specific in this specific endpoint so in this case as you have seen everything here as as meant by the controller uh is is going to be into the games pad right so in here the pattern the follow is the name of the controller in the controller's name gains and that's why in games at HTTP we can reach everything by using games right slash games so that same part is the one that we're going to be using for our minimal API so let's go down here and we're going to be using that A Game's pad over there and then the next thing is going to be the request delegate so this is the the place where we Define the actual action to execute when somebody hits that specific endpoint so to define the delegate all you have to do is just to Define it as a very standard delegate actually like that right and now we have a place to actually execute the action now before running the the actual code here we're going to be needing one thing which is going to be the repository the repository instance so let's go ahead and do dependency injection not even Constructor but in this case in this delegate directly there's going to be an i games repository like that and then for this repository we're going to name it just as repository and now we are pretty much ready to execute the code that code is going to be the same thing as in the controller so let's go down into our games controller and let's just copy that line of code that we have over here right so let's copy that enter from CS like this and then what you're going to need is to introduce one morning space hello API identities so we can actually use this method which all it's going to do is transform the entity into a dto that we can then return back into the client right and by doing this we have pretty much implemented our conduction as you can see at least no more than just I guess four lines of code right there's no need to define a class there is no need for Constructor and there is no need even for an attribute it's all defined right here and this is one of the things I really like about minimum apis because it is very focused very specific right so we are defining four lines of code specifically for the get action so it's a very nice way to get started with minimal apis and so that is our first action so let's move forward with the next one which is going to be the one to retrieve Again by ID and let me actually collapse this for a second like that so to Define this action it's going to be very similar to the previous one so let's go ahead and do app that market and just like before it's going to start with the games a prefix but in this case we do need to specify what's going to be the ID right as part of the PAD as a correct address in implementation so it's going to be ID and then we also need to specify again that request delegate so let's go ahead and Define that delegate over here right just like that and then we do need two things here so first we need our I gains repository right repository in this case we're also going to be needing that specific ID for the game to retrieve so it's going to be int ID like that and then we're ready to go ahead and implement this endpoint just like that so let's go back into controller and let's copy these few lines over here let's copy that and as you can see it's a very standard implementation that all it does is says okay let's go ahead and retrieve the game and if we are not able to find it it means that I mean it's not found it's not there in a repository now in this case we cannot do just not found we have to do results that not found that's a title return for minimal apis and then for the other case we're actually able to find the the object we're going to do is just say results that okay and in that okay we're going to be passing in that detail that we're going to return back into the client there are other types to tourists to use here right but we're not going to recover that in this video now um but yeah that's pretty much your get by ID implementation as you can see very straightforward same code but with less ceremony I guess now one thing that may be bothered you at this point is that we are going to start running too much code in our product CS class which it should should really just meant for a bootstrapping application right not for writing all of that that code so in that case we can start doing a couple of things so the first thing is that we can uh centralize how we Define this prefix for our endpoints and we can do that by using a features is known as route groups so let me show you that let me scroll down here and we can do now is just say the following bar group is it going to be up that map group right and in that map group you can Define what's going to be the prefix for all of the endpoints in that group so in this case we know that the prefix that we've been using is game so I'm just going to be copying this over there and now we have defined a group where we can add all of the endpoints so instead of using the app object directly we're going to be copying group and using it right here and right here right so both of them are going to start there and therefore we don't need to specify a prefix anymore we can remove this and just Define our rainbows like this by the way that group is actually very powerful and you can attach a lot of other things that you want to be common for all the endpoints so that's the first thing and then the other thing is that perhaps like I said we don't want to have so much code in prom.cs so what we can do is just move all this into kind of an extension method in another place so that we can keep operand.cs very clean so let's go ahead and do that and we're going to do is just create a brand new folder at the root a new folder let's let's call this yes endpoints right and here we're going to be creating a brand new new class let's call this one games and points all right and this is going to be a static class static class let's clean up things a little bit over here all right and so in this method in this class we're going to be defining just one method which is a method to map all of our routes so this is going to be public static and then we're going to be returning the actual group instance back into the color so it's going to be route group Builder and then let's call this one map games and then we're going to be extending our I endpoint route Builder and let's call this one routes all right so that's our method over there and then really all we have to do there is just move this code that we have over here let's let's copy this and then paste it over there like that let me left it so we can see better okay and then we have to do a few things so instead of using App we're going to be using routes over here and then we have to import the namespace for our repository like that all right we also need a namespace for sdto and then finally we're going to be returning our group like this okay and just by doing that now we have a center place to just lay out all of our endpoints for the minimum API and so with that in place we can go back into brown.cs and we can do instead of running all of this code over here we can just say now app that not games right like this and then of course we need any space there like that okay and now our our process is a very very clean as you can see so we can focus our development now in games endpoint over here so now we have endpoints for the get actions let's also do the the post right so let's go back and let's actually yeah let's actually go down here and we're going to be defining a brand new method in our group which is going to be so group dot in this case is going to be map post right and so again we're going to define the route it's just going to start with our slash because we have the group that defines the prefix and then again we want to specify what's going to be our delegated right so let's go ahead and add our delegate over here all right and then two things we're going to need here first as as usual we are going to need our eye gains capacitor instance right there and then the other thing is going to be the dto that represents the payload that the client is going to send us here so in this case that the dto is going to be create game video and then its name is one game video right we're going to need one new space over here it's going to be hello.api.dos like that and then we're ready to go ahead and Implement our pulse action over there so for the actual logic of this pulse action list as usual let's go back into games controller over there and let's see what we have before so let's go down here for post this is logic so let's go ahead and copy this these lines of code into games endpoints over here right there and then let's see what we have to change right so it's very standard so we go ahead and create an entity out of the dto right so I'm going to say okay go ahead and do the create uh but in this case we cannot use just a grid adduction over here we have to use instead of that create add route that's the right method to use and then as usual we want to specify and of course yeah it's going to be results.graded route and then in this case we cannot just say name of get because such a get method doesn't exist right so in the case of the controller we could say name of cat because get is the method defined over here right is that name uh but in this case we cannot do that so what we're going to do instead is just add a name for our get by ID and endpoint and then we can refer that name over here so to do that let's go down and over here into account by ID and what we're going to do is just say here with name and then we can just attach any any name here let's say get by ID okay so that's going to be our gate by the endpoint and by doing that we can now copy that name and just use it down here into Nissan over there right so now this is this is going to create a location header using the route of the gate by the endpoint right and the rest is pretty standard as you can see and now now that we have the post implementation let's go ahead and Implement our put so let's go down here and let's go ahead and Implement group dot map put and then in this case and once again we're going to start with Slash and then we're going to be receiving the daddy like that uh ID and then again we're going to be needing a delegate here so let's define the delegate like this and then let's go ahead and Define uh actually let's fix this like that all right and then for the put again we're going to be needing our repository right so I games Repository and in this case we're going to be needing the dto for the updates right so in that case that's going to be a update game dto and then let's call this one game video and then one more thing we're going to need here is the actual ID of the game to update so it's going to be in ID all right so that's going to be for our put and let's scroll down this so we can see better there and then let's go back in the controller let's see what kind of implementation we had before it's going to be this one here let's copy that into games endpoints over here there and again it's a very standard stuff first we'll try to retrieve the game if we cannot find it we're going to do not found so it's going to be results that not found and then uh here we have we'll be using updated game videos so let's actually rename game video as updated game video like that and then finally we're going to be returning no content so it's going to be results that no content like that and that's pretty much our put implementation all right so let's move forward with the next one which is going to be our delete so group dot map delete right and then delete is a is again is a very very simple implementation it's going to be uh for our pad it's going to be just ID and then again we're going to need our delegate over here so let's go ahead and Define that over there it's a delegate and then here we're going to be needing again our I gains Repository and every also going to be needing the ID right so in ID like this and then for the actual implementation let's go back into games controllers and then let's see what we implemented for delete it's going to be these few lines over there let's paste that over here and now we have our delete implementation which again is just try to find the game if it cannot find it it's going to get I mean if it isn't if it is able to find it it will go ahead and delete it but if you cannot find it it doesn't really matter because for delete what we want to make sure is that we always do the same thing it's kind of ident right so it doesn't matter if the resource exists or not we just go ahead and delete it if we find it and otherwise we just say yeah we are done with the action with the request so it's going to be here results that the content like this that is pretty much the end of this set of endpoints for minimal apis right that's how you define them and then one more thing to do here is go back into RAM CS right and we have to do just a couple of changes here so remember that in the case of controllers we are defining this line here to add all the controllers that are found uh in the in the project or in the assembly but we are not not going to be using those anymore so I'm going to just remove that line right and we also don't need this other line which just maps all the controllers into the proper routes right uh but we're not going to be using controllers so we're going to remove that line over there and with that we're pretty much ready to try out our our minimal API so let's go down into our terminal and let's go ahead and do it on the run and let's see if things are working as expected just as before so let's go into our games.http file over here and let's collapse things a little bit let's see what happens if we try to go ahead and now use our games input over here so send requests so as expected it is working just fine so we are able to retrieve our five games as you can see we have our fine game somewhere there we can retrieve game let's say game number three yeah FIFA 23 is right there we are able to retrieve it and then let's see if we can create our Minecraft game so Minecraft game via a post actions and requests yes and we are even getting the location header right there right which in fact I'm going to be using a to retrieve game number six we're just gonna do a Minecraft game let's see if there it is there so it has been created successfully and then can we update game number one so let's send a request for a put Returns the content but now our game number one should be Street Fighter to Turbo so let's see uh in fact uh yeah we have three Fighter 2 turbo and the price has also been updated so update is working just fine and lastly let's see if we can delete game number five so send request okay number five let's try to retrieve game number five now and then it is not there so it's not found so the the exact same behavior we had before we controllers now we have with our minimal API so that's working great uh but there's one more thing here which is going to be the case of the validation the input validation and this is one that will not come and just work by default in minimal apis because let me show you what happens so if we just remove the the name from this from this game here for the Post action and I try to do a send request we're going to find is that the game is actually created right so you can see that the status code here was created and the game was created but kind of with a null name which is not not great right so that didn't work by default uh but it that's actually very easy to to Handle by adding just an open source and nougat package so let me show you this uh let's go ahead and down into our terminal what you want to do is just introduce a brand new nugget package it's going to be dotnet add package and the package is named minimal API star exchanges right so if you just add that that's going to go ahead and bring some new functionality that takes advantage of feature called endpoint filters to be able to filter the data that comes into any of our endpoints so how do we take advantage of that package just go back into games endpoints over here and what you want to do is in the definition of your group right here you can just add one more line that's going to say with parameter validation by doing that you now have input validation for all of the endpoints you find it in that one group right just with that one line so if you now try again and run our API just like that let's go back to case.hdp and collapses and then if we try to execute this post once again with no name send request and as you can see we now get our expected Bud request over here and it clearly says that the name field is required right because validation has been enabled be a diffusion like I said is called import filters and so yeah that now that we are pretty much done with this let me start my service on my server let's see how this this ends up being so now we have all of running points pretty much well defined in this file over here and we don't need our games controller anymore so we can go ahead and delete controllers like that let's go ahead and delete this and now we have completely moved into the new minimal apis approach which as you can as you can see it is very straightforward very simple to write and it is like I said it is very very focused right so you clearly say what you want to do for each of the endpoints as opposed to how to rely on kind of a convention which is what was the previous air controllers approach so consider this new approach for your next.net race API and if you want this useful please check out my next video we recovered another topic that is also essential for professional.net developers and it's also thanks for watching and I'll see you next time
Info
Channel: Julio Casal
Views: 2,612
Rating: undefined out of 5
Keywords:
Id: Zu_8CVc_Ozc
Channel Id: undefined
Length: 17min 37sec (1057 seconds)
Published: Tue May 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.