HILT Field Injection and Constructor Injection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when it comes to dependency injection there's two types of injection that you can do or typically there's two types two types that I even know about number one is field injection this is kind of the simpler way to do things there's not as many limitations or required workarounds that you need to get it done it's kind of like the simple way I guess that's the way to think of it the second way is constructor injection and constructor injection is the way you definitely want to do it whenever possible you definitely want to do constructor injection over field injection whenever possible the main reason for that is with constructor injection you're passing parameters through the constructor therefore when that object gets instantiated you know exactly what that object needs this is great for your production code you know if you accentuate an object you're passing it to the constructor you need to pass the things that it needs to work properly also when you're testing so if you're writing tests if you're building mocks your building fakes when you know what that object requires it becomes much easier and this is kind of one of the most important core concepts of dependency injection or why you use dependency injection in the first place so in this video what I'm going to do is I'm going to show you examples of both I'm gonna show you filled injection kind of what it is how to use it with hilt I'm gonna show you constructor injection how to use it with hilt and how to set up your activities your fragments whatever classes that you're going to be injecting dependencies into when you're using hilt okay so if you've been following along in the previous video we set up our application class we annotated it with at hilt Android app and got all of our dependencies that we need to get started using held so we can close the application class now we're going to go into main activity and I'm going to declare some dependencies and we're gonna field inject them into main activity so first things first we need to annotate this with a special hilt annotation it's going to be Android entry point now this is a new hilt thing so for those of you who are familiar with dagger you did not have access to this annotation when when using dagger so what this does is it basically basically replaces the need to declare main activity as something that you can inject it into in the component and I'll give you an example so if we go back to I'm just gonna go to some other source code that I worked on in the past where I was working on I used dagger in this project so if you're familiar with dagger you know that you had to declare like this app component and by the way if you're not familiar with dagger and you have no idea what I'm showing you on the screen don't worry it's not necessarily important I'm just trying to show this to the people who do have experience with dagger so remember you have like your app component you have like some dependencies some modules the factory pattern to instantiate it whatever then down here if you wanted to inject anything into main activity or into any fragments or anything you had to add this kind of an injection and add that that class to the dagger graph so that you can inject things into it so what this this Android entry point does here is it basically removes the need for that so much like in our application class we just added this simple annotation so that it generates a component kind of gets everything started it's the same kind of thing with this android entry point so we just annotate this with an android entry point and we don't have to add this this basically adds it to the dagger graph so that now I can inject dependencies into it that's essentially what it does so now for those of you who have no dependency injection knowledge let's declare some dependencies and actually inject these into main activity so I'm just gonna build like some random class I'll just call it I'll just do like class you know some class and - to mark this as something that is injectable or to add this as a as a dependency for dagger for hilt we mark the constructor with at inject so I'm going to do a deject then add a constructor I'm not gonna pass anything to the constructor I'm just simply adding it so I can get rid of these spaces if that's confusing you I'm not gonna extend anything I'm just going to open this up and there we go we have a class that is gonna be supplied by dagger because it has this inject ons on the constructor so I'm gonna create a simple function inside of this class say do a thing it's just gonna do a thing it's going to return some string and I'm gonna write return look I did a thing so this dependency isn't doing much it's just a class that has a single function when that function is called it's going to return a string so now I'm going to come up into my main activity and I'll take this with add inject too late and knit var and insert mahou some class that dependency that I just declared so so what have I done here I've created a class just like some random class not important I've annotated the constructor with that inject I've now done this is called field injection so field injection as opposed to constructor injection which we haven't looked at yet and I'm injecting this into main activity as a dependency now so what dagger does in the background is it's going to instantiate this class and kind of hold it in memory so that it can be injected and used by other classes and that's what's happening here in main activity so if I go down here and I just write like print line and I'll write some it doesn't really matter I'll just write you know some class which is the dependency and called do a thing now if I was to run this I'm gonna press the play button up here and if we take a look at the log output what we should see is look I did a thing okay so I've pulled out the log I filtered on look and there it's a system out look I did a thing so that means that our dependency is working correctly so this might not seem that amazing to you but there's a lot going on behind the scenes what up because what it's what's dagger doing it's it's creating this dependency at compile time and it's it's making it available to me at runtime and then I can inject it into classes and I can use it so this is one this is one type of dependency injection filled injection now we're going to look at the second type which is constructor injection so I'm going to create another class I'm gonna scroll down here and create another class this will just be called you know some other class it doesn't really matter again I'm going to annotate the constructor with ADD inject to tell dagger that this is going to be a dependency you need to generate this object at compile time because I need it I'm gonna be using it that's a habit for me to make that constructor big because usually you're passing things I'll just keep it small now because we don't have any arguments now I'm gonna create another function called function do some other thing it's gonna do the same thing kind of return a string so do return look I did some other thing and I'll do exclamation mark so now what we're going to do is pass this class as a dependency to this other class so inside of the constructor I can write private whoops that's not the constructor up in the constructor I can write private value some other class and get a get a reference to that class so what I just did there this is considered constructor injection when you have a dependency like this one we know it's a dependency because it's marked with ADD inject on the constructor and then I pass that argument to the constructor here now you may be wondering you know how does this object getting past here because we're never actually instantiating the object anywhere we're just like literally passing it as a constructor argument well what's happening behind the scenes is that compile-time dagger is doing all of this for you it's going to create an instance of some other class it's going to build that then it's going to create an instance of some class and pass that instance of some other class to the constructor here so what we can do is create a function in your function I'll say do some other thing do some other thing it will also return a string now this one will return some other class dot do some other thing so I'm getting a reference to the function that's in that other class and I'm going to gonna use it so now I'm gonna come up to the top here and do print line I'll do some class dot do some other thing and what we should see in the log is first of all look I did a thing and then after that we should see look I did some other thing so let's let's run that and take a look at the log output oh whoops look like looks like I got a typo here this should be print line print line I'm gonna run that and we'll take a look so there's a log output filtering on look I get look I did a thing and then look I did some other thing so I'm sure that if you have no experience with dagger this is going to look quite confusing to you all so pointless because this is like a very abstract example like what is some class and what is some other class what's the point of that don't worry about that if you're confused just kind of put that to the side for now the thing that I wanted you to really notice here is just the difference between field injection which is this type of injection up here we have inject it has to be a late and NIT variable not a value and then just the class and the end constructor injection so constructor injection is then passing it through the constructor all I wanted you to know is the difference between those two things and then of course if we want a class to be to be able to have dependencies injected into it we have to annotate it with Android entry point those are the three point things that I wanted you to see and just kind of understand or grasp so don't worry if you're confused about the rest in the next video I'm going to talk about scoping and the tier like system of the different scopes and the different components that hilt makes available to you also if you haven't yet go to coding with Mitch komm register an account join the mailing list and you'll get notifications for when new new videos and new courses are released also this course if you're watching on YouTube you can watch it on my website and I in my opinion it's much easier to watch it on my website because it'll track your progress it'll keep track of like did you watch there's this video how far did you get in that video and then it'll just generally keep keep track of it of the whole course as a whole as opposed to YouTube which is just a playlist so go do that if you haven't and I'll see you in the next video you
Info
Channel: CodingWithMitch
Views: 25,061
Rating: undefined out of 5
Keywords: dagger2, dagger-android, dependency injection android, android dependency injection, dagger android, hilt android, android hilt tutorial, android dagger hilt, android dependency injection with dagger2, dagger 2 kotlin, dagger 2 kotlin tutorial, dagger 2 kotlin mvvm, dagger 2 kotlin android, dagger hilt
Id: pZJzdgG_45Y
Channel Id: undefined
Length: 9min 44sec (584 seconds)
Published: Tue Jul 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.