Adding Dependency Injection to WPF applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back to the codewinkers channel for a new video this time we'll talk about wpf applications yes you heard it right i usually talk about asp.net core and things uh well related to the web and to the cloud but recently i started to dig deeper into okay how can i develop a desktop application because i really have um i don't have a lot of experience developing desktop applications so i just wanted to dig deeper into this topic and one of the first things that i encountered or topics that uh that uh well sprung my curiosity was how can i use a dependency injection container in a wpf application because the idea of having such a container with services that you can resolve wherever you need them in controls or in windows or on pages or things like that is very very appealing from my point of view because it solves a lot of problems so i was thinking how can i get this done in a wpf application and at first i was thinking that okay i might need to use some third-party libraries like castle winsor or an inject or other common libraries that we would normally use in regular regular.net framework applications when we want to implement the dependency injection container but then i was giving a deeper thought about this and since wpf applications are also available on.net core and in fact what you see here on the screen is a wpf application on.net core and there are some advantages of developing wpf applications on.net core and one of the advantages is that you can actually leverage the the advantages that the platform offers to you in order to implement the standard dependency injection container that you commonly use in asp.net core apps and of course uh i was thinking since we have a generic host in dot net core this is a very important topic because i guess until dot net core 2.2 or 2.1 there was only a web host in in dot net core and the web post was of course configured to be used on web applications so it was not suitable for w or it wouldn't be suitable for wpf applications but then dotnet core introduced the concept of generic hosts where you can build actually your own hosts and dotnet core provides even a default host that is not tied to any web technology that you can actually use to run your applications so my idea for this walkthrough is to simply start using disadvantages implement a default host in our wpf application and in that default host of course we could use the dependency injection container that we would have in that specific host so that's actually what we want to do right now in this video so let's get started so at first i will take a look here in this uh app.xaml.cs and at summer if you're not very familiar with w with wpf applications this is actually the entry point in your application and from my point of view this is also the best place where you could actually create your host runner host and of course uh configured the services that you need for the dependency injection container so let's get started with it but before we can uh get uh to code things we have to install a dependency and then if i go here to manage nuget packages i will browse and there is this package microsoft extensions hosting and the cool thing about this hosting package is that it actually contains a lot of the other packages that are needed like logging like services and things like that so if we install this we actually have everything that we need to implement our own host here in our wpf application and of course start using the dependency injection container for that so let's just install this and we will accept everything that they want to install those were all the packages that that it basically installs or that are part of of this library microsoft extensions hosting and they said that's actually everything that we will need right now so everything is installed but now that we have uh everything set up like the dependency that we need we can go back to our app.saml.cs file and we can start basically implement all the things that we will need what i would like to have here is first a private field uh let's make it also read only and it would be of type i host and let's call it host [Music] and then what we would need is of course a constructor where we would get everything going and before we get uh further with the constructor uh there are some other methods that i would like to use here so similar to what we know in is or with what we are familiar in asp.net core i want to add a configure services method and let's uh of course we'll make this private and it would be void for return void it would be called configure services sorry for all the typos here and of course here in the services what we would need is to get the service collection i service collection services and everything should be fine and of course just a little bit later we'll implement what we want here but then uh two other things that i want to do i want to override some methods on app which is on exit and on start so it would be a protected override i think void on start on startup i guess it's called yeah it should be something like that let me check again if everything is okay here we would also need to have a startup event args event rxe and yeah that should be fine right now so what do we want to do when the application starts well first of all we want to start our host and in this case we would have here like a wait host start async we'll start this host asynchronously uh that should do it and then uh there are some other things that we might do here a little bit later but for now based on startup uh we go back to the base startup methods and we have to pass the arguments there of course and similarly to what we have here i would like to add also for on exit protected override async void on exit i guess it is and here we need the exit event args exit event rxe that should do it and what we want to do here is uh let's uh let's have using and let's have this host here and then now what we want to actually do here is stop the host wait so wait uh host or stop async and here we have to uh specify also a time span or no we we don't need to re-specify that of course it should be like that and uh then after we do this we can then go out to the base on exit and then pass the event arcs so this is to just make sure that we start our host when the application starts and that we basically stop our host when the application stops so yeah now that we have this very very basic setup here we can concentrate on creating the host itself and configure the services of course so let's do this of course in the constructor in this case let's define okay the host would be equal to host and for now we'll create a default host builder or we'll use the default host builder and then here we have we have the configure services method on the default host builder and here uh we could of course pass in a funk uh we would get the context we would get the services and then of course we can do this here or it should at least be possible to do that and then we want to build the host so that's actually what what we want to do and here we can actually say what what we want to do and here is we call the configure uh services method that we created earlier so the private one and we pass in the services that we actually get so this is the service collection and then here in this configure services we can just start like we do in asp.net core to add the services that we want here now one important thing that i want to do here is and i guess we'll see why just in a second services add singleton and what i will do i will add the main window as a service to this dependency to this dependency injection container now why i do this is because then in this startup what we could do is after we start the host we can just then show the window and here we would have a far let's see main window equals and here would be a host services get required service where is it here it is uh of type main window [Music] and the cool thing here is that you can then do something like main window dot show and from my point of view this is a very very nice thing because you can add the windows that you have in your applications to this dependency uh injection container and inject them wherever you need and you can show those windows in different parts of of the applications very very easily now one thing that we have to take into consideration uh is of course that uh there are some things that we would still have to change in the main window here and here in the main window let's uh let's go back to this main window what we want to do here is uh first of all well we can initialize the component and everything i guess should be fine right now but i will add something just a little bit later for now i just want to make sure that this uh that these things work and here if i go to the app.saml what i want to do is here is the startup uri and specify the main window which should be the startup window however since i am showing this window directly from the code behind i don't need this anymore here so we can simply remove this and it actually should still work let's start the application and let's check things out hopefully we don't get any error uh now we get here the main window of course we don't have uh or we don't display things here in the main window it is a completely blank one but we'll do some changes here just in a second for now it's important to notice that it actually works and the way that we actually show this window is simply that uh in this app.saml.cs in the on startup we basically resolve the main window that we added as a service in configure services and then we showed it so it was not showing uh because here we had that startup uri because we removed it but the window is showing because we show it here we resolve it as a service and then we can show it and that's basically it now to give a little bit more context and to to to to have some some deeper insights on how real services might work with that let's even just go one step forward and create a very very basic service together with an interface so let's create an interface uh where do we have your interface and let's call this i date time service and in this interface we'll expose only one method that returns a string and uh this would be called something like get date time string so that would be the interface and let's add also an implementation for that let's add a new class i will call this date time service and in this class we'll implement the service so let's uh we say that it implements i date time service and then we have to implement this here and the only thing that we will do here it's very very easy like more a dummy service a return date time i'll get daytime now to long day stream so this is what we returned from this service actually okay so now that we have all this uh type of interface and service what we can do is of course go here and add also this service to the dependency injection container so it would be services at the transient i would say here in this case uh the interface is i date time service and the implementation is date time service [Music] so that should do it now the fact that we have this service here uh what it actually does for us is that we can right now go in the main window and if we go here we can just uh well rearrange or do some some some other stuff here in this main window first of all i would like a field private read-only field to hold the idata service because we'll inject this service here uh similarly to the way that we inject services in controllers in uh asp.net core so it would be i date time service date time service so we can simply say that in our constructor we want the i date time service of course after we initialize the component uh sorry uh the the the window what we can do here is say that the date time service equals date time service and that should do it and then of course we want a property where we will get the date time public string date time and we'll have here a getter and in this getter we will just return the date from the service datetime service get date time string and that would be it and of course since we are we want to bind this to a to a text box uh so we will also use a setter here but we want uh we won't place uh things in the setter uh we we just need it because we need to do data binding and there is one other thing that we would have to do here is to set the data context in order to have data binding so it would be i can say this data context and this data context should be uh this so the windows itself but we can even skip this because it's it's cleared it's the data context and it should still be fine now i see that i have an error here and i'm not sure exactly why of course i missed of course i missed a semicolon yeah rookie mistakes happen all the time and right now that we have everything set up here we can actually go in this main window and uh well add basically a text box here it would be a very very simple one here in this grid we would basically use let's do it like that to be sure that that we close it so it would be uh text box let's uh let's give it a name the name should be date time and we should then uh specify the text and the text would be a binding to our date time property hopefully it is the correct one okay and right now i would expect that when i start the application i should see a text box actually where i display the date time from the service that we actually injected in this main window in the constructor just like we do in regular asp.net core apps so let's see if this is working right now so it's starting and indeed we see here the date and the time uh which is correct so what we accomplished right now is that we created a sorry not a web host but a default host uh in the app app.saml.cs file and since we have created a host we are able to configure the services or in other words add services to the dependency injection container and once we add services to the di container we can inject them actually wherever we need in our case we injected the i dates time service just like we do in regular asp.net core controllers for instance but in this case we inject these services in the main window and we use that the service to get some some data in this case some very very dummy data and to to display it to bind it in the ui in a text box in this case now before we wrap this up is there's one more thing that i want to say is that when we create a host here here we just create a default host which adds a different type of things here like logging like reading phone configuration files and things like that and uh of course we can use this default uh this default host but we can even customize the host that the way that we need so we can add different uh settings files if we need we can add different loggers to the application and things like that but i don't want to make this video too long and i promise you well soon after this one i will do the next video where we will go on and configure our host for a wpf application to add some logging to it and of course to also uh to to also define uh more advanced services and to work with configuration files and configuration and settings and things like that because it's really really really important but once you get the the idea that the concept on on what the host is actually in dot net core you can see that it's very easy to implement it even in in a wpf application and in my case so in this very very specific use case my goal was to be able to use the dependency injection container in order to add my services and resolve the services wherever i need in this application so this being said uh that's it for this video thank you very much for watching uh if you enjoyed this content don't forget to hit the subscribe button uh to hit the thumbs up button and if you're if you're not shy and i hope you're not trying uh even please feel free and drop a comment uh right below this video it would be very very important to me for me to to get your feedback once again thanks thank you very much for watching and until the next time i wish you only the best
Info
Channel: Codewrinkles
Views: 2,657
Rating: undefined out of 5
Keywords: DotNet, DotNetCore, WPF, Programming, CSharp, C#, Desktop apps
Id: XW_qgbUg1ZI
Channel Id: undefined
Length: 22min 41sec (1361 seconds)
Published: Mon Nov 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.