Dependency Injection in Minimal APIs in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
minimal apis are an increasingly common way to create small API projects including microservices but how do you handle dependency injection with minimal API it turns out the answer to that is rather simple in this video I will show you how to use dependency injection along with route parameters and More in 10 minutes or less now for most of my training I work to get an in-depth perspective on technology but sometimes you just need the quick answer to the question how do I use this that's why I created this 10 minute training Series so let's Dive Right In and you can get this code using the link in the description if you want to follow along now what I've done is I've taken a minimal API project and I've really trimmed it down to just the bare necessities so if we look at the program.cs there's very little in this and in fact all the code is in just one file so I have here the creation of the app the user redirect and the run those are the three lines that come from the template everything else is the custom stuff I have two different routes here and I have one record and that's it that's the entire API we have a git which gets a specific user ID now this of course is faking that because we're passing an ID and then we are returning a record that says here's the ID the first and the last name then we have a post which we say okay when I post this um this endpoint and we want to pass in a person record and we just return that person record which again is kind of Faking it but we just want to create something simple now I created I took out the Swagger stuff I took all that out and instead I created just an HTTP file which allows us to test this out so I pass in one call there is the get to just the the number five that's gonna return back the record ID with an ID of five and then this one here is going to pass in a record with an ID of zero which what you normally do and then a first and last name of Mary Smith so let's run this API and of course it goes to a 404 can't be found because there is no root page here no problem but I can run our our HTTP file we can do the get which says get and the ID is five first and last name is Tim Corey and then we do the post here and we see the ID is zero the first and last name is Mary Smith so that's working now let's start to work with dependency injection because that's where we want to you know figure out how to work with the pansy Jackson we already have stuff being passed into our method how you add depends the injection well the simple answer is we just ask for it configuration so we're going to ask for the eye configuration whoops eye configuration I configuration and we'll just call this config and this comes from depanzee injection this is built into the defaults for our app builder so with depends injection and in play and I can use configuration I can get to app settings which I have this test info object here with ID first name and last name so we're passing an ID here let's instead of returning Tim Curry we can say return and let's just do each on its own line here and we'll say instead of Tim will say config dot get get value I'll type string and this is test info first name notice the colon there allows you to get to the next level deep and we were to say yes it's going to have value I don't don't want you to yell at me for that and then we'll do the same thing here for the last name so that pulls from our app settings now and we have first name and last name being pulled by just passing in the request for dependency injection but we're mixing this in with this which comes from the route and if we run this let's wait for it to load okay it's loaded go back to our call here for our git and now we see that we're returning not only the ID number they passed in but also now first and last name is Sue storm instead of Tim Corey because we pulled that from our app settings so that's how we can use dependency injection with a minimal API but what if we're using a post command where we're not passing anything out of the URL but we're pulling this from the body well again no problem we can just ask for eye configuration and the system is smart enough to figure out oh that comes from depends the injection I'll pull it from there and now we can say let's say int ID equals config dot get value for test info ID like as our integer we can say VAR new person equals person with idea of our ID if you remember it's a record so we can't just say person.id equals we can create a new person a new uh instance with the ID overwritten with the ideas passed in so now or I'm sorry with the king from the configuration now I can say new person is returned if we were to run this and then we go back to our HTTP file go to our post we can see that we run it we get the value of 23 for the ID of Mary Smith y23 because that's the ID we passed in so it's simulating we're passing in a record and we're still getting the value back to the record so we know it's was passed in properly but we are updating the ID number to be from the configuration which is coming in through dependency injection so we can mix and match these things pretty liberally pretty easily and to say hey get this and it figures out oh is this the body is this the the uh query parameter or is this from the configuration it figures out where all is go and it gets the right one and it hooks it up four so just pass into your method as a parameter it will figure it out now when you're pulling out into a separate class and with you have a class and you have each of these methods just remember that each of these methods gets dependency injection as opposed to the class getting depends the injection so just the method does it's a little bit different but we have the method pulling in from the panzy injection as well as from the body as well as from the route and so on so that's how you use the pansy injection in minimal apis in.net six and above all right thanks for watching and as always I am Tim Corey [Music] [Applause] [Music]
Info
Channel: IAmTimCorey
Views: 16,957
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6
Id: i-xJ97uJ9qY
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Mon Jul 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.