Coding Shorts: Prototyping with Minimal APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to coding shorts i'm sean wildermuth a few videos back i talked about minimal apis and net 6 and asp.net core today we're going to reintroduce that idea of minimal apis but i want to talk about it in the way of prototyping apis this is important to me because while minimal apis i think are going to be great for microservices for larger projects i think you can still use minimal apis to prototype your projects to prototype the apis that those projects are going to use it's a great way to get started without having to deal with a lot of the complexity and then later upgrade to a full nbc api experience let's take a look [Music] now that we've reached net 6 preview 7 let's talk a little bit more about minimal apis i'm in visual studio 2022 with the preview installed and i've got pretty much an empty web app that i've generated right and essentially it has not a lot of information in it mostly in the program.cs and i've built as we can see over here just a quick database that we're going to use we're not quite going to use it yet because i want to kind of talk about how this all looks if we're used to using startup we might be confused about exactly where we want to do services and by default we've been using dependency injection for these services the trick here is before you build the app you're going to be given that opportunity by looking at the builder services this is where you would add let's say transient foo right you could go ahead and do that more likely you're going to add existing ones like let's say cores if we needed cores in here right that's how we would add these things we would add them into the dependency injection layer to continue doing what we've always done which is using constructors for classes or even with minimal apis being able to use parameters to the callbacks to specify what the types we need so here is where we set up the service container right and then later if we need to say app.use course which isn't going to do anything in this case we haven't built up what we're going to allow but for now that'll just tell us that hey we can go ahead and use these now and they'll do the same thing we did before and obviously if we run this [Applause] refresh our page we can see it just returns that hello world just like we see here right map get root let's create another one because what i want to use minimal apis is not just to create microservices where if you have a number of small services that do some discrete things i'm not sure that this gets us any less complexity than when we finally get into mvc but what i do want to use it for and i suspect i'll be using this a lot for is to be able to prototype my apis think beginning of the project or green field or the start of a new sort of project i can start with the mental api and then graduate to full controllers as it makes sense because our problem here is if we had let's say people here and i'm not going to do anything interesting in here yet just return some empties right is that i would then have to have a bunch of these for the people post put and delete and these can have a lot of code in them right i mean obviously i'm not doing anything with the callbacks yet but this can get a little complicated and trying to do them all in here or even in their own class that does all these maps for us i think it can get a bit complicated really quickly but for prototyping our api i think it makes a lot of sense i still like the one controller for one sort of entity or noun in the api idea where you'd be able to do the get post put delete as well as any other related entities and to use controllers to handle things like navigations through our api but i cover all that in courses and stuff i'm not going to get too deep into this what i do want to say is that you know if we're using something like this let's go ahead and add builder.services for something we're actually going to use and i'm going to add a db context and that type is going to be a i think i called it a people context let's see what i did here in data people context and then down here into my lambda i can just say people context context let's put those in parentheses so that it knows what i want injected in here then i can just say context people to list async and we'll just make sure this is a sync and a weight right we're still getting the same behavior i hate this new dialogue and visual studio 2020 by the way and let's continue to make edits i've made all these edits and let's see if this actually works over here api people and so here we're seeing api people we can see we're getting data back in that way we want to and these are just entities there's nothing really magical about them they're just what are stored in my database stupid little database just to show all of you but one of the things that is possible here is that we don't need dependency injection at all right what happens if what we really want here is to be able to create the context and just leave it in scope here because remember this is all one scope this is effectively a function scope right and so instead of adding it here and instead of expecting it here right i gotta figure out how to turn this off stop the application and what we're gonna do is actually build this and so what am i gonna do i'm gonna go ahead and create our contact by saying new people context right but of course it doesn't take an empty constructor in my case it expects a configuration and this is why using the service collection can be useful but if we take this out of the idea of just database access there might be a bunch of objects here that you can just have in scope let's go ahead and just complete this idea i'm going to go ahead and create config by just saying the app we've built let's go ahead and get the service for eye configuration and then we can just pass it into here or handling our own construction in this case right and then this continues to work because this is a closure and this run is just setting up all these callbacks and of course the help didn't really help me it's services.getservice yeah there we go and let's see if this runs i'm going to run it again refresh and we can see it returns that same thing and it will continue to do this because what we're not getting here is we're not getting this reuse of this a single object but if we're creating these microservices maybe it doesn't matter this certainly doesn't leave the connection open so we can easily leak this and this just becomes like a singletons right the singleton or anything else you need in all of this code just becomes part of that this doesn't mean you shouldn't use this using the dependency injection layer just when i'm prototyping maybe i don't want to go through all that trouble more maybe when i'm building microservices i don't want to go to all that trouble sometimes it might just be easier especially if you have things that are hard to put into a dependency injection like you might have static objects or return singletons and ways that aren't easily serviceable by the service injection layer just go ahead and do this the point of this isn't telling you how you should be doing this the point of this is telling you that you can do it this is opening up different ways that we're going to construct our applications especially if they're small applications for me this becomes fairly simple because all i want to really do is use this for prototyping of my larger more complex projects marrying this almost single file.net project with creation of a view or an angular app to me makes prototyping and building up the beginnings of projects pretty simple and then we can start to refactor because remember refactoring is the cheap way of adding more manageability to our project but allowing us to get to that first prototype as fast as possible sometimes creating that whole structure of controllers just to see whether an idea has merit isn't really useful and this is super useful for doing that hope that makes sense this has been a coding short my name is shawn wildermuth of wilderminds thanks for watching [Music] [Music] you
Info
Channel: swildermuth
Views: 478
Rating: 5 out of 5
Keywords: Programming, Software Development, C#, .NET, DOTNET, .NET 6, ASP.NET, ASP.NET Core, Minimal APIs, Global Usings, Top-Level Statements
Id: 5pCufXfBWFU
Channel Id: undefined
Length: 9min 11sec (551 seconds)
Published: Wed Sep 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.