Hilt Modules, Binds and Provides (Dagger2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another dagger hilt video in the previous video we worked on showing some scenarios when you have difficulties injecting dependencies so more specifically you can't inject an interface or inject something that implements an interface and this kind of scenario comes up all the time like if you don't own a third-party library you want to provide that as a dependency you can't just inject it you have to do something special likewise if you have an interface that you know stubs out some functions you implement that interface inside of a class you can't provide that class implementation just out of the box you can't just annotate it without inject on the constructor and expect dagger to know how to build it you have to do something special so in this video I'm going to show you those special things that you can do and for some of you who already have a little bit of experience with dagger this is gonna be we're gonna work on the app provides annotation the binds annotation and building dagger modules and installing them into components and I'm also gonna highlight something that I think that hilt the hilt documentation doesn't do a very good job of explaining you know I'm a big fan of hilt I only I've only been using it for a while but it definitely makes using dagger much simpler I think the documentation is mostly really good but in this video I'm gonna show you something that I don't really like that much about it and I think they definitely could have done a better job of explaining in the documentation so just to kind of review really quickly what we did in the previous video we have like a couple classes here so we have some class and it's annotated without inject so it's being provided as a dependency we have some interface way down at the bottom here which is it just has a single function which is being implemented in some interface implementation we tried to to provide this through dagger by annotating without inject but we get a compile time error up here because dagger does not know how to build this object because we're telling it it's an interface but actually it's an implementation it doesn't it doesn't know how to build this likewise with the JSON object this JSON object is from a third-party library it's from the retrofit converter jisang converted library so dagger does not know how to build this object so we we if we were to run this if I just press the play button I'm gonna get a compile time error saying that hey I don't know what this is and I don't know what this is so now this video we're going to work on providing these dependencies so that dagger knows how to build these objects so information on this stuff you can find in the hilt documentation under hilt modules so here's the link up here but you can just click on the side menu here that says hilt modules so if you look over here there's a couple options here you can say inject interface instances with app binds inject interface instances but that provides provide oh that's that's a separate thing so these two right here this inject interface instances with binds and then provide so there's two kind of options here and this is what I think the hilt documentation does not do a good job here of because you have two options here and I'm you know even after reading through this I'm really not sure like which one to use which ones using which scenarios and which one to eat which one is better which one is more performant so basically I was confused and I and I know how to use dagger I'm experienced with dagger so like what does it look like for a person who's not experienced with dagger so what I'm gonna do here is instead of just reading through the documentation and going through it with you I designed an example to illustrate the the questions that I had and the questions that you probably would have encountered if you were to go through this documentation as I did so let's go take a look at the examples and I'm going to show you kind of basically what happens here's we have we have two methods we can use binds or we can use provides to provide these these dependencies I'm going to show you the that one of them is much simpler than the other one and works in all scenarios and one of them is more complex and doesn't work in every scenario so obviously I hope like me by the end of this video you will choose to use the one that is simpler and can be used in all scenarios because obviously that's the right choice okay so what we're gonna do here is we're gonna start with the more complex way and surprise surprise the more complex way is actually the way that doesn't work in all scenarios so I think this is very strange that they even have this method but anyway let's go through it and later I'll talk more about it and get my thoughts on it so let's start by building the module class and every module class gets annotated with app module now keep in mind I could have gone you know to the main package directory right click created a new file and call this you know whatever my module is gonna be but just to keep things simple and keep everything in one file so I can you know easily move to different stuff and highlight different features I'm just gonna put everything in this in this one file in main activity so this class is going to be called whoops it needs to be an abstract class abstract class I'll just call it my module and again remember that this is the more complex way there's a much more simple way that we're going to look at after this so I'm gonna annotate this with singleton because why not you don't really need to annotate it with anything we're not you know if you don't want to scope it you don't have to but I'm just gonna use the singleton scope just because it's the most common one that people use and now we're going to use the annotation binds so remember we have to kind of to kind of options we have provides I'll just write provides up here and we also have at binds add vines those are the two options so the first one is binds and that's the more complicated way and that's the one that we're looking at right now so abstract function find you know some dependency I'll call it this will take some implementation as input I'll say some interface as input and then we want to out pay our sorry we want to actually this should be implementation some interface implementation and we want to output some interface so what i'm doing here is i'm telling hilt how to build this object so remember if we go up here into our some class we have two dependencies we have the jisan which I'm actually going to comment out for now because we're gonna strictly just kind of focus on I'll actually just delete it we're gonna focus on the interface remember there's two issues here we we don't know how to build interface objects and we don't know how to build objects from third-party libraries so we're focusing on just the interface one for now so right now I'm building this module and I'm telling I'm telling hilt how to build this object and this is what I mean by its it's more complex if you just look at this notation I think it's it's weird like it just like I don't know having the abstract functions having this this is the object that it's building I just think the notation of this is confusing but there's some code generation that goes on behind the scenes and it does actually know how to build this object just keep in mind again that this is the more complex way I promise the simpler way is way more obvious and I don't know why that it's just not the standard out of the box so anyway finish up this module I want to annotate this with at install in and I want to install it into my application component so that this is another hilt feature remember when I said that there's there's different components I'll actually pull up a chart here so here are the different hilt components we have you know the application component activity retained component activity component and so on and they have their respective scopes and if you scroll down you can see the component scopes right here remember the singleton is the application activity retained scope is the view model and so on and so on so in in hilts we we tell it what what component to install that module into so because i'm installing it into the application component we know that these dependencies are going to exist as long as the application is alive if I wanted to install it into the activity component I could do that but it's only going to be alive as long as the activity that it lives in in this case we are actually injecting into an activity so that would be totally fine but if I was to change this to like a fragment component and I was to try and run this it would give me a compile time error so anyway let's actually change this back to application component I'm gonna press the green the green play button up here and we're gonna run it and I'll show you that yes hilt does know how to build this dependency now and we're going to be able to inject the some interface implementation all right so there I just want to show you on the right the app is running and we do not get a compile time error so that does work we're able to build this this some interface implementation by using this kind of weird Bynes notation now let's explore the different components so like I said if I was to changes to activity component it would have no problem since we are injecting this into an activity so I'm going to run that again and I'm going to bring the finished activity onto the screen of the finished app on the screen and whoa look at that we actually do get a compile time error now if you look at this think about it and see if you can figure out why we got a compile time error I'll give you a couple seconds here if you think that it is the scope you are correct this needs to be changed to activity scope if you're installing something into the activity component you can't you can't scope it to the as a singleton because the activity scoped objects and this activity component doesn't have any knowledge of the application component remember if we take a look at the chart so I'll bring it up again it's kind of like a tier system right so like the activity scope and the activity component doesn't know that the application component exists because it's it's higher up in the hierarchy whereas the application component does know about this it does know about this it goes downwards so what I just tried to do there is I tried to create a dependency that was scoped to a singleton or in other words meant for the app component and I tried to install it into an activity component which is no good so if I change the scope to activity scope and change the component to activity component and I try running that again we should be fine and everything should work just as we intended and there you see you get the app on the screen everything's running no compile time errors so that's all good ok so now so now you've kind of been introduced to this this weird binds annotation for providing dependencies that I personally don't like because I just don't like the notation of it now let me show you that not only is it complicated but it doesn't work for all scenarios so let's go to the top here and I'm gonna add that jisan dependency again so private value JSON JSON so I'm just trying to tell it to inject this JSON object and now I'm gonna use the same kind of same kind of notation here for providing a jisan dependency because theoretically that's the purpose of binds it's its to put a leg or to build things that it doesn't know how to build so I'm gonna try and do that I'm gonna annotate this with you know app binds binds and I'll do you know abstract function find jisan and then I'm going to do the object in here so that would mean that I would pass the JSON object just kind of following the the general notation that it that the guideline tells me to follow so there we go and now if I run this I am going to get a compile time error so there we go there's a compile time error the app does not run so that's option 1 option 1 is the more complicated way we're using the binds annotation we're providing dependencies using the binds annotation but number 1 I think the notation is very complex I don't think it's like easy to read I don't think it's intuitive I think it's it's just not great is how I would describe it and number 2 is it doesn't work in all scenarios the main point of providing dependencies this way is to tell dagger how to build things that it doesn't know how to build because you don't have access to it or its implementing an interface or something like that so not only is it complex it doesn't cover all of the scenarios so now what we're going to do is we're going to look at the other way which is using app binds and I'm going to that not only is it much easier but it can work in all scenarios so let's go take a look at that okay so most things are gonna be the same here except we want to remove abstract we can I'll just delete these because we'll kind of restart so we still want install into the activity component we can also install into the activity retained component or we can install into the application component because those are all things that sit above the activity inside of the the tier system that exists so either way it doesn't matter now let's use add provides well we can do you know singleton just to show a scope whatever now I'm going to do app provides and then just do fun provide the some interface implementation now the dependency for some interface implementation is what so if we come up here and we look at some interface implementation actually you know what let's do some let's call this some interface because conventionally usually you don't have the implementation to the provides but that's that's not that important so anyway what are the dependencies for some interface implementation well there isn't any notice there's nothing being passed as a constructor argument here so we didn't we know we don't have any dependency so I can just do two back spaces there and here I want to return some interface so notice I'm returning the interface but now I'm going to return the actual implementation so some interface implementation and then just initialize that and that is all you need to do so you can see that this is much simpler than the binds function I think and if you had requirements on this dependency you would just pass them through the constructor so just kind of as an example you know maybe I had some dependency here some dependency I'll just do like whoops this should be a value private value some dependency and maybe this is just some string then all you would do is come down into your module you can do at singleton you know add provides I can do provide some string this will return a string and then just do return some string so now this dependency is being provided through the module and I can just do you know some string some string and string and then dagger will know where to get that from and then where to to use that you know totally totally just as an example obviously I'm not using this usually you would use that in the class that you were injected into I just want to show you an example well just to make it more clear I guess I could do like a thing let's do a thing and then just add the string here just because I don't know why not just to actually use it so anyway you can see that this this notation is much simpler it's very clear like what it's doing it's providing a dependency where the constructor arguments are going what object its building it just looks it looks much more clear to me so now let's do the JSON object so singleton you know it provides fun I can just do provide jisan I want to turn return JSON and then just return a new JSON object very clear what's happening here you know it's it's providing something the function outlines what it's providing it's returning this object here I am instantiated object boom there you go so now let's run this and let's see if we get any compile time errors all right so there you go we have the finished version of the app on the screen here and it compiles there is no errors so there you have it those are the two functions using app binds app provides to provide dependencies to solve that issue of you know injecting interfaces injecting things that you don't own from third-party libraries and you can pretty clearly see that one is definitely more complex than the other one it doesn't work in all scenarios one is much simpler and does work in all scenarios and again I want to say that if there's anything I can say bad about hilt it's that this part of the documentation was not explained correctly you know personally right now I don't even know like is using binds better for performance then using provides I have no idea I'm sure there must be some difference you know actually if you know comment down below because I don't know I couldn't find anywhere in the documentation that said like binds is better or provides is better I don't know it just looked like they were showing me two methods to do the same thing and one worked in all scenarios and one did not so if you know something that I don't please comment down below so anyway that's gonna be it for this video I've basically shown you all of the really kind of bare essentials when it comes to dagger you can you know everything now from doing basic constructor injection providing dependencies for classes that you own providing dependencies or providing things that you don't own providing things that in use interfaces the different scopes the different it's how they're how they're set up so really like the bare basics of hilt is done now and that's that's what I mean by it's it's much simpler than dagger there's no way I could have explained the basics of dagger in like what did I use like five videos or something and I don't think any of them are longer than ten minutes long so so tilt is definitely I get I give it a thumbs up it is definitely simpler to use than dagger the Android team definitely achieved their goal of making dagger simpler and easier to use for developers so now for the rest of this kind of Health course I'm going to work on actual real examples so I'm going to build out some architecture probably gonna use clean architecture you know we're gonna have activities we're gonna have a fragment fragment or two maybe we're gonna have view models we're gonna do constructor injection into the view models we're gonna have I'm not going to use a repository instead I'm gonna use data sources which essentially is a repository or they are repositories it's the same thing you know you'll pass your retrofit instance into the data source you'll pass your caching you're Dao into the data source pass those into the view models it's the same thing it's the same kind of architecture basically it's it's gonna be mvvm because we don't really need MDI because it's just not going to be a big sample so it's going to be a little basic example using mvvm clean architecture and hilt just so you can see kind of everything packaged together I'm gonna do like a basic network request where you hit the hit the network get some data then cache that data into the room persistence library and just show you kind of the the most common scenarios that you're gonna run into when it comes to architecture and using dependency injection now before you go do not forget to like the video if you don't tell YouTube that you like these videos it does not recommend it to other developers who need help learning about hilt so please like the video go down there leave and leave a comment give me some kind of engagement I would really appreciate it and also don't forget to check out coding with Mitch comm where you can watch this course and many other courses lots of them are free even you don't got to pay for it and it'll track your progress so like how much of a video you watch if you watch the whole course all that kind of good stuff thanks for watching and I'll see you in the next one you
Info
Channel: CodingWithMitch
Views: 12,921
Rating: 4.9634147 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, dagger modules, hilt modules
Id: KI3L6d6Sm3Q
Channel Id: undefined
Length: 18min 2sec (1082 seconds)
Published: Wed Jul 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.