The simplest way to create an API is with .NET | Minimal APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this i'm going to show you what is possibly the simplest way to create an api in any programming language and any framework and we're going to do that with c-sharp and.net 6 which is currently in preview now i know that sounds weird because dotnet historically has been notorious for kind of a lot of boilerplate code when it comes down to building apis but with net six there have been some great improvements that allow this to actually happen and really the only minimal approach that comes close to this that i've personally seen is node.js express approach which usually comes down to four to five lines of code but with this we're gonna be even less than that and it's also very scalable and also very performant and i'm gonna explain a lot of more things as i'm going through the code if you like a lot of content and you want to see more make sure you subscribe with the certification bell to get related when uploading new video so here i have visual studio you don't have to use visual studio you can use any id that you want but this one for now in preview supports the preview features coming out in november in that dot net 6 version so i'm going to be using that and i'm going to show you everything it takes to create a very very easy and simple api that just prints hello world when you access the root endpoint so first i want to create an app and i can do that by using the web application dot create builder and i'm gonna grab the arguments from the console application because this acts basically as a console application which exposes an api and then i'm going to do a build to create that app then i'm going to say app.map get and i'm going to map the root endpoint so just forward slash here to return hello world here you go and then all i'm gonna do is app.run that's it that's an api you can run this you can publish it it can be a fully self-contained app if you wanted to so you don't actually have to install net anywhere for this to run you could just publish it in the same way you publish a go app which is a purely search contained exe you can do that here but you can also just install the runtime on wherever you wanna run this the dotnet runtime that is and then you don't have to publish it for specific architectures so there's a benefit to that and also the overall build size is way way way smaller this is a very small app to build and as you can see this might not look like something you've seen before if you've been exposed to csharpen.net in the past and that is because in.net six there's tons of features that have been added that allow us to write code like this and let me just quickly show you this thing actually running so i'm gonna go ahead and say um build and run this simple api and this api is now running and i'm going to go here on pause button i can click send and as you can see this thing actually responds with hello world oh and just to show you another cool feature coming in dot net 6 i can say hello world reloaded here i haven't stopped the app the app is still running here i can change that i can hot reload and now if i go back to postman the app did not restart it's still in debugging mode but it is responding with the new text so i can hot reload while i'm debugging which is very very useful when you're working or debugging on an api now that's cool enough in itself and app has a ton of other autocomplete approaches and things you can do with it so it's very very flexible very very extensive they basically abstracted a lot of the things you shouldn't need to worry about and they give you just the nice things to play with and build your applications now looking at this you must be saying oh nick microsoft is obviously hiding a lot of stuff and there's tons of files that i need to copy around for this to actually be functional so please give me the truth well i'll give you the truth if i go into the folder in file explorer these are all the files there are to the project and really the only two files that you really worry about is this program.cs and this is everything that there is in this program.cs there's nothing else in here and then the other thing that you really need is this cs approach here which if i show you what it contains that's all there is to it nine lines of code actually seven but yeah that's all you need there is nothing else that you need for this api to actually run and i can prove that because i can take just these two files the simple api and the program.cs i'm gonna go at the very top and make a new folder here i'm gonna call it test and then i'm gonna paste those files so these are the two things that i said we need again this and what you saw before in that xml and then i'm gonna start a powershell console here i'm gonna do.net run it is gonna create a bunch of things to run it but this thing is now running from that console that i just created and you can see it's responding fine so those are the only two things you need there is no hidden magic there when you eventually run it the magic will come into the picture but those are not files that you commit into your github or whatever now how does this compare to the old style of doing things and what i'm going to do is i'm going to run this old style api and i'm going to show you the functional side of things first what does it actually do so we have a customer's endpoint which will return all the customers currently we have no customers so what i'm going to do is i'm gonna create using this body a new customer with my name and i get an id back so this is a crowd api basically now i can go here and i can get that customer by id or i'm gonna go back and list all customers then i can go here and update this customer with a different name and then if i list again you can see that there is a new name here and then i can go here and delete this customer and if i go back to listing there is nothing here so this is a very simple very basic crud api how does that work in the old style of doing apis in dot net let's see that first so first we have the customer controller here and you just delete that and you have using statements you have name spaces you have attributes here to specify the routing then you have to extend classes um we're injecting a custom repository and then we have the endpoints get all get by id create update you can see that it's lengthy it's around 60 lines just for the controller and the controller if you've worked with any mvc framework before is effectively our entry point for our request here the routing comes through that then here's where we have the model so it's a class a customer class with the id and the full name and then we have a repository in this example we're using a dictionary as any member database but it's easy enough to swap that out for an orm and the code doesn't really change that much if we use something like entity framework and then you can see what that code looks like another 50 lines here then an interface to make that testable then we have the program.cs which if you remember how it compares to the previous one it's night and day and then we have the startup.cs where a lot of the bootstrapping lives so you can see add controllers add singleton a bunch of middlewares and some routing configuration so lots of boilerplate around 200 lines of code for a simple crud api now what i want to do is i want to go here in this simple api example and i'm going to just uh delete that and i'm going to build the same api the same crowd api for customers but using the minimal api approach of net 6. so first we need a builder not an app because this time we're going to have to do some more configuration on the thing especially around dependency injection so i'm gonna say arguments here i'm gonna create the builder and then i'm gonna create an app so the app is builder dot build simple enough and then i'm gonna say app.run and that's it that's the basic building block to run my application now you might remember that here we have a customer class with id and full name to do that here i'm going to use a record instead it's a more immutable type of a class if you think about it and all i need to do is customer open brackets good id capital i and string will name that's it and i have my type and then the other thing i need is a customer repository to manage that data now this doesn't really change from the old system so i'm going to go ahead and just copy paste that over here i added an extra angle bracket and i'm going to delete this interface for this example i don't need it so now we have a repository to manage the data acting as the data access layer to our database fake database in this example and now first i want to register this uh repository in my dependency injection container because i don't want to manage like instantiating all that and life cycles and all that for this service so i'm going to go builder.services.add singleton and i'm going to say add the custom repository and manage it as a singleton in my application's lifetime simple enough and now what i can do is i can say app.map get and i'm going to map my first endpoint which is going to be the customer's endpoint and this endpoint needs to return all the customers back to the user so how do i do that well all i need to do is say from services to indicate that i need something from the service container the dependency injection container so i need the customer repository i'm going to call it repo and then arrow function and this is where my method is going to return so what i need to do is unfortunately use a using statement to add this because this is not just automatically added so it is adding a line of code up here and then all i need to do is say return repo dot get all that's it and now i have my endpoint now compare this to this it's clearly less code it's almost half the code and it is doing the exact same thing very very cool and then i'm gonna go ahead and map all the other endpoints which will work in a very similar fashion for example to get one by id i need to do map get and then customers first class and i'm going to use this idle bracket syntax which means that this id is a route parameter and what i need to do now is coma and i'm still going to use this from services thing but i also going to use the good id and that's all i need really the rest will be just automatically done for me so i'm gonna move this back here and then to return by id customer i'm gonna say customer equals repo dot get by id and i'm gonna put the id that i get from the parameter here which is this parameter simple enough and then because if it's null i want to return and not found what i'm going to do is return customer is not null because when it is not null we want to actually return results dot okay an ok request with the customer object in it else we want to return the results.not found method which will return a 404 so if i go back and i quickly execute that and i go back to postman all the way here and i click send i'm getting a 404 back because that thing doesn't actually exist next we have to actually create a customer so i'm going to say map post post is used to create so i'm going to copy that customer's text here and i'm going to do a very similar thing again but instead of using an id in the route instead i'm gonna just say customer which will read a customer from the request body so this object here dotnet knows that hey there's something coming in the request body map it to that object if you can so first we're going to use a repo to create that customer and then we want to return that it was created and in a restful manner we want to say results.created and we want to specify where that thing was created so it can be added in the location uri header so i'm going to say customers first slash customer dot id because that's how you access that customer and then the object itself will go into the body as well and that's all you need for this thing to work and then i'm going to fast forward to the last two things the update and the delete because they're using principles from what we already seen here whether that is uh body mapping or rod parameter mapping or dependency injection just quickly show you how this is all wired up together so as you can see the update is here it uses both a body parameter and the uri or route parameter and then dependency injection to check whether the customer exists or not and return the right thing and then delete here as well and really that's all there is to it i'm gonna go ahead and just run this simple api again i'm gonna go back here and let's run through the full flow again from scratch so no customers here i'm gonna go ahead and create a new customer so nick was created i'm gonna grab the id and i can go back now and see that nick is in here then i'm gonna try to get that non-existing one first by id this should return a 404 and surely enough it is and then i'm gonna paste the new id of the customer that exists and i'm gonna get them back and i'm gonna go here and update the name to tom chaps's so tom subscribers sure enough and now i'm gonna go ahead and delete it and now it should no longer be here full crowd api with literally just i don't know 86 lines of code including a repository and keep in mind that if you want to plug a real database here with empty framework this would only add roughly 10 lines of code so you would have full api 2 database storage and management in under 100 likes of code which is insane for any language and dotnet specifically coming from that background of being a more boilerplate heavy language now the tech empowered benchmarks for json for this new approach so that this can do roughly 800 000 requests per second and this is something like 40 more than the previous 500 000 requests per second of the controller based approach now you should still be using that controller-based approach if you need a more structured solution especially in enterprise applications but if you're just starting building a quick api that can still scale and this is all compliant with async and a weight by the way you can totally have your usual async await um solution here and benefit from um that a single weight scale then you can definitely use that as a production approach nothing stops you and you're gonna be totally fine with great performance and great scalability again this is all coming out in november as an lts release and microsoft is still testing and listening for ideas and approaches for all this i think it's brilliant and i highly recommend you check that out and give your feedback that's all i have for you for this video thank you very much for watching special thanks to my patreons for making videos possible if you want to support me as well you can find a link in the description down below leave a like if you like this video subscribe for more content like discerning the bell as well and i'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 44,449
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, clean code, dotnet, simple api, the simplest api, how to build an api, minimal api, minimal apis .net 6, .net 6, asp.net core 6, The simplest way to create an API is with .NET
Id: eRJFNGIsJEo
Channel Id: undefined
Length: 15min 17sec (917 seconds)
Published: Tue Aug 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.