This Is How Simple Dependency Injection is in .NET MAUI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you want to learn all about.net maui and dependency injection you've come to the right place my friend come right through so here we are in visual studio 2022 um this is a file new.net maui application at the time of recording this is done at maui preview 9 but i'm kind of sure that this won't change too much if dot maui goes to ga because this follows the same pattern as the asp.net dependency injection and the microsoft extensions one so um you know we're trying to follow the same pattern with don and maui as well um so i think this is kind of final uh maybe some other things that you're seeing here might change but the code i think should be mostly the same if it gets outdated please let me know in the comments ping me and i'll be sure to make an updated video just for you but here this is a file new.net maui application and when i run this let's just run it the uwp application or i should say the win ui application you will see the default template hello maui hello world welcome to dot and maui with a button and the dot net bot so that's all good now let me close this one because i'm going to change some code let's go over to our solution explorer right here to our maui mauiprogram.cs which is our entry point for um our.net maui application and here you can see the generic host builder i've talked about that in previous videos on.net maui updates that when this got implemented and this again follows the same pattern as like your asp.net application where you kind of like build the host all the configuration all the stuff that you can use from inside of your application so here we are saying like hey use maui app and we specify the class the app class that it should use to [Music] bootstrap our.net maui app or we can say configure fonts or if we go in here you can see there is configure effects or configure maui handlers or some other stuff in here as well so there's all kinds of things here let me remove this line here and if we go down here now if you don't know what dependency injection is dependency injection i got a video on that so you it should pop up on your screen right now or you can find the link down below in the video description i'm going to assume that you know the basics already if you don't please pause this video go over to the other video and i'll be waiting here for you to come back for the rest but dependency injection is now something that is built inside of don and maui no need for extra plugins or libraries or whatever you can just use it from right here which is really cool and it's built into this generic host builder so if we now do builder we also have services because they are registered as a service and it is a i surface collection just like you would know from like the microsoft extensions or asp.net i'm gonna mention asp.net a lot during this video so and here you can say dot add but what is even cooler is that you can also use the scopes right so then we should add singleton if we want to add a singleton here we need to just bring in a extra using statement here so that it understands that this is in here because this is set up as an extension method so i'm going to hover over it and click the little light bulb here for intellisense and it's going to say actually using microsoft extensions dependency injection so that is built in is just using that and now we can register our services as add singleton or add a hosted service or logging or options or scoped or transient all these options that you know from dependency injection and that you might already have been using so this is also a little pro tip right here if you are starting uh or anticipating on.net maui but you're still doing examined forms application or you're starting a xamarin forms application right now maybe you can use the microsoft extensions dependency injection library right now with xamarin forms and if you do that it will make it easier for you to transfer to don and maui by the time that you feel confident that it is the time to actually convert to dot at maui so that's a little tip from my side right here now let's add this singles in here and i'm going to work now with the things that i already have so i'm just going to say the main page because the main page is already something that is in our application in the default template so let me just do that i'm going to register the main page let's actually just focus on that first and i'm going to show you how to take it a step further even right after that so we've registered the main page now as a singleton and what i can do then is go over to that actual app class right here so let's find that in our solution explorer our app.example and actually we have to go to our app example.cs and whenever we go in here you can see it does this new main page now if you know a thing or two about dependency injection you know that you shouldn't be doing new main page or new any object because that is something that you don't want you want to resolve and everything from your container so what we can do now is just use this constructor injection right here and say main page page and i can say not new main page but i can say page right here and because everything is wired up here in this maui program with this host builder because we are registering it right here and it says use maui app it will now automatically see hey do i have this main page in my container yes i have let's inject it in the constructor in this app object right here and it's going to set the main page to page whenever i set it to the right name here and not the object one and it's going to inject it here and it's going to use that page so whenever i run it now you will see nothing has changed but the thing to note here is that it actually works so i've registered my page in the dependency injection container it will inject that into our app object and it will now resolve that automatically and show us our app while i remove the new main page call right so that's very cool now again let's i said let's take this a step further because now from here because this is resolved this way it will also kind of cascade or trickle down to all the the other things that will be resolved here so if we go into our main page let's go over to our solution explorer our main page code behind so our mainpage.example.com and here in my constructor for the main page i'm going to say main page view model doesn't exist yet so i'm going to have to create that and let's name this view model and we can set the binding context so we're going to work with a little bit of data binding here binding context is view model you can see visual studio 2022 already knows what i'm doing so just press tab to accept and there it is all my code so now it's going to wire up this binding context to my view model and i can just use that and i don't have to do new main page view model or whatever it will just see it because it's registered in my dependency injection container well it isn't just quite yet so let's just do that right now um first let's create our main page view model so i'm going to go over to my solution explorer again right click on my project add new class i'm going to name that the main page view model ad so now we should see a little class that's being generated let's make this public else it will start complaining and let's add a property here so let's add public string message get here we go and let's make this what should we make this maybe something maybe something specific to your favorite youtuber um so let's say maybe did you subscribe to gerald's youtube yet huh huh so maybe you should take a moment and do that right now so here we have just a simple arbitrary message right it's in a property it's in our view model this should be if this is a real live app it's probably going to be much much more complicated than this but i'm just i'm here to show you the dependency injection not how to build your app so let's go back to our maui program and now kind of like all i need to do here is duplicate this line and say main page view model and it should register that and i think well actually let's should be able to show our message so let's go to our mainpage.example and instead of saying welcome to net multi-platform app ui we're going to say binding message because now we have this message in our view model and i'm going to run this again and then if everything goes right you can see i didn't change anything else i just injected that view model in our main page constructor and boom our message is here did you subscribed oh i got a typo right here and to gerald's youtube yet but you can see it works this way so that's how easy it is to wire up the dependency injection from within a dotnet maui application now i already mentioned it a couple of times this is very similar to other.net solutions so asp.net maybe i don't know if other solutions are using this but um it shouldn't matter if you're doing desktop development or web development or mobile app development we're trying to get that friction out of there for you so that it's easier for you to learn the stuff that is very specific to the platform because let's be honest there is still a lot to learn if you're doing web you need to learn css html if you're doing mobile app development then you need to still learn something about how that behaves because there is always going to be something specific to the platform that you're deploying to but the easier we can make it for you to onboard with like arbitrary stuff like dependency injection and whatnot if we can take that away for you um then we're doing a good job right then it will be the same for all applications and it will be easier for you to start and um go make great speeds at making wonderful apps so that's kind of like the idea behind all of this um now i already created some content for dependency injection so go check out the playlist for that on this channel if there are any questions of course feel free to leave them down in the comments below and i'll be sure to answer them or maybe make a video especially for you for the rest please click that like button if you've actually liked this video because that will help spread this video to a wider audience and ultimately will then help me and this channel grow a little bit so i can continue to do this and bring you this wonderful wonderful content also be sure to check out the subscribe button if you didn't see my subtle hint earlier and maybe you want to check out if that subscribe button is lit up so you will be notified of new content automatically and i will come to you so you don't have to come to me for the rest keep coding
Info
Channel: Gerald Versluis
Views: 2,862
Rating: undefined out of 5
Keywords: .net maui, dotnet maui, .net 6, .net maui example, .net maui tutorial, .net 6 xamarin, dotnet maui tutorial, dependency injection, dotnet maui getting started, .net dependency injection tutorial, .net dependency injection, microsoft extensions dependency injection, generic host builder, .net 6.0, .net 6 maui preview, dotnet 6, hands on, .net maui mobile, dependency injection c#
Id: wkgbvMlrMhU
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Thu Oct 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.