Dependency injection from zero to hero with Koin by Arnaud Giuliani EN

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay hello everyone do you hear me in the another room thank you for being for this session about coin I'm an OG&E I'm a French freelance developer I'm also the COI lead project and we see about coin project today but before diving into source code stuff just I will tell you about the story of coin and a first question that usually people ask me is why why are you crazy enough to try to write a new dependency injection framework yeah police let's get back in 2016 and 2016 we have the first production ready of culling and because it's far isn't ready I've put in production so for my haps with totally net it transpiration ready and now it's so easy to tell people that cutting clearly bring you so much stuff for you daily development stuff that Korea when you develop with cotton you feel like a superhero but we still use some stuff that rely on ecosystem Java okay that mainly you can use dagger also stuff or if you don't use Android you can make some spring but mainly this stuff is driven by the Java language and this Java language is build on really I would say interesting process interesting concept but with cotton it likely is some kind of boring to you to use day by day I think I'm kind of boring to use a notation proxying stuff like that I think that now we can try to make something better perhaps a French stuff I didn't put any relational picture then but yeah and almost you can feel that you have no need of the eye framework when you begin to make some cotton stuff because you can you have the object you have functioned you have generates you have delegates you can't write fun stuff and so try to begin your di stuff bind your own but it's quite complex to assume it everyday then I try to look at something simple for me because I like simple solution I don't want to look at documentation I just want to look at a simple snippet I just want to read and understand it and simple not in the meaning of something stupid for me clearly simple is clearly the key of what I'm writing everyday and this is with this that I begin to look to any kind of solution around and didn't found what I'm looking for and I'm looking for something that clearly is oriented for my need justly my need all daily stuff really simple stuff for writing my components not too much oriented with drivin theory or just over waving by over feature that I don't know I don't care and I just don't want to use that sure the simple stuff and then I got the idea that finally and the feeling that finally when I'm writing something like that I can clearly describe this stuff by function that describe those two components and I have somewhere a link between those two functions and clearly what is something that is needed here is a link to make a function calling another one and resolving the stuff and this is the main idea of coin to illustrate the stuff there's debate a bit of stuff to do that is just take an example and perhaps a well-known example from the daggers developers let's check it together and let's see how what is this app because yeah you can see many post of her and you can see many article about the thermosyphon example but this is just a coffee maker okay this is just first class that tried to make the coffee and we have a bump the demos hi first stuff and we have a heater okay to heat the water lots of complex the thing is that we want to demonstrate some di concept that for I would say in a primary usage of corn or any di language AI framework is just to say that we can inject components dependencies by the abstraction by the interface okay then yeah to try make a bit harder we make a link between the thermosyphon hand the heater because when we make the coffee we hung we want to pump the water only if it's hot okay let's check that in a pure code finn-style okay no di no frame or just go clean as you would write it in your pure cotton style that we have the other each interface it's quite simple and we have on off that make the the heating on off and then we can check if the heating is hot and you will see that this is the most complex class that we have the and then when we run we what you want when we want to ride the pump here the thermo siphon stuff we will make naturally we will use some constrictor injection stuff that we here we use the ether by interface and then we make the pumping process and we are checking if it's hot then okay not so complex and the last one of those three classes we make constructor injection to say okay I use the palm I use the heater and I can do my coffee yay then it's not so complex and we we need a final stuff we need a final class to bootstrap everything to run the stuff to run those components that we will declare somewhere and we need to get the coffee maker stuff and just run run it and run the process okay and here is the question how we assemble it let's do this with coin that brings an additive dsl to describe it a lightweight container that think about something that run your functions because we will use functions to describe the components and also we will have a simple API just to let you use your instance in the most easy way as you can to do that we just use the coin core dependency Gradle and here you can see that it's completely agnostic and then I can just run my example directly with Gradle and this little dependency the first thing that you will do then is to describe a module to describe all your definitions okay and then I use module function and I will describe my definitions here okay and what I will use is mainly singer or factory that mean I I want to describe a single term our Factory is something I want to rebel when I has to have this instance and mainly behind that we will use a functional expression to build the component that we want that mean that mainly we will call constrictive of all classes here let's take the example and let's describe our app just with three lines for a with coin okay for these three components let's start with the electric eater and then let's declare the first stuff that is a singer of electric heater with decrying a single turn of area okay and because coin don't want to provide any introspection stuff and just want to make explicit stuff and explicit running components we have to specify here that we want to use this component has the eater interface type okay we have to be explicit there's no magic if we don't want any introspection if we want to do clearly smoothing stuff with functional we just render functions and then we have to carry give some clues to coin to resolve the stuff the same for the time or cellphone here we just give a single of pump for the time most often time of - stuff but the main interesting thing is that here this is directly a DSL according the SL that mean that your code is compiling directly in your in your IDE and then you will have directly the compiler saying hey there's something missing here that mean that this is my class I said that I have a dependency then I have to give something for the for the dependency and here you have the false keyword that you need here this is we want we have we will have going to get the instance here and because cotton is smart enough to guess the type we don't need to specify anything anytime that we want to resolve and then this is how you describe components and you just reserve dependency between them last of the last component the coffeemaker will be the same at it we need the two components the dependency resolution here and we just writing code structure to to get functions you can write it in one module or in a separate modules we don't have any import or linking stuff we rely mainly on dynamic weasel resolution and from the first version to the current state I would say that nobody asked me to or asked in a kind of way are asked to any kind of import or leaking functions people are happy with just giving the modules and running the modules what we can do because we are completely dynamic is to check the configuration of our module to be sure that yeah everything is dynamic but we want to be sure that the graph is is ok and we are not missing something as in the definitions another stuff behind that once you have declared your components in the DSL you will rely on API and this API is also described as the current component and a coin component it will be a class for you that is allowed to use the coin container in detail coin component is an interface okay you don't have to implement anything in just a marker for us to allow to identify a link to the a coin API for you because you clearly see here that you have a link to the coin API when you describe the stuff in the DSL you don't have any link to coin and this is one of the first errs the first the first idea that we had at the beginning of Kony is that we want to be able to just throw away coin if it's not good enough and come back to another thing we want to be completely honest he can have the ability to declare the components completely in the independent way from any framework but this coin component interface allow us to give you those API that are inject and get and for us then this is the key component that letters bootstrap any kind of environment and any kind of runtime like kata like Android and give these components feature directly to any kind of an runtime environment and I mean what that means that here I just need to tie my component with current component interface right and I can use the by inject the decade stuff and that's it finally we have two runs runs rent coin from somewhere and we have to start the container the thing that will read your definitions and allow you to resolve everything and you just start from a main point and we just use the start point function and this tapcon function this is the point two zero way of decrying your coin container that means that here it's a kind of small DSL to just declare the starting component that you will need to for your container and your app and here we just need to declare modules and if you have several modules yeah please do that and then we have started coins we can run in the instance for a class and reuse all of stuff and make the coffee laughs thing is that if we clearly respect the exercise and we want to go to the end of the stuff we have a lazy link here that mean that for us we have to make a lazy resolution of this component and what we do here is that we rely completely of the country on the country language we don't reinvent the wheel and then it all us to just you have instead of my type my original type I've either lazy of hitter and then use it by delegate when I want it then I don't have to use the some get value when I need it this is just the cutting type of delay and the lazy type here that mean that when you write stuff with coin and the DSL we just give some clues for you to help build the stuff but after that you have the power in your hands to write anything you want let's get back a bit on the dagger version please because I would just give you some clues to get away from dagger and have some key stuff to migrate you up easily from dagger to coin and just to refresh your memory in this app we should have a module that declare a single tone here of heater and we leave another module the pump module here that binds the stuff and binds the other instance and finally when we want to bootstrap to render in the running components we have to tag it like that and saying that is a single turn and we have to import the drip coffee module okay and after that you have also to inject audio constructor to be sure that everything is injected here you can also notice that we are using the dagger could lazy type that means that it's not so cutting friendly when when you want to make some things matter okay finally yes sorry just migrated to coin what else how do that we will just identify the smallest components of the graph and then we will make the bridge between the two world step by step and then injecting in dagger components start from the from coin and how we do that we will make component that we make the bridge on the component that we need to retrieve either components from dagger and either component from coil let's declare our heater component and in the dagger class what we will do that we will move the property as delegated by coin directly we don't make a constructor injection here because we don't need it anymore and also in the other class we will just make an exit delegate injection from coin and then from that you have the key to make in advance everything and migrate component by component and that's it you have migrated your component if you want there's a catch-up link to have the complete article here to have the full details of how we can migrate dagger thermosyphon example to coin and how we complete completely make all the graph dependency being injected by coin let's check what coin brings clearly for your Android platform and first of that is we can't just create our activities of fragments clearly like that and we have to rely on life cycle but coin will give you carry coin extensions for your Android components and clearly make those components has the where coin component endures out of the box that means what that mean that you just have to include one Gradle dibalan see I have to start you come you your point dependency in modules somewhere from your application in the Android way don't forget to had android logo and android context that means that 4.20 we have to be express it in the way that we want the stuff to be injected and reused for the starting process then if you miss the line and read context you will miss to inject your Android context in your corner cell and then once you have started coin in your main application you just open your activity and when you want to the end in a dependency you just did by inject and that's it you don't and you don't have anything to do somewhere you don't have to do anything else if you want to get them the Android context from the DSL because this is where sometimes you need to inject the Android context it's not good because then you are linked to Android context and it's hard to test but then sometimes we need what you will do is use the Android context function that will reserve the stuff for you and disturb the Android context functions resolve the Unrated context for you but you can also use the get resolution function that make the resolution eric resolution for you dealing with transient components for example presenters it's kind of easy because then we will just rely on the factoring that means that we will have going to create an instance when we need it we don't want singer we don't want to have single dose of or presenter and then when we hear when we will have our activity that will be created at the first call then we will ask for the injection of the presenter and then create the instance for us and then each time you recreate each time you has for the new presenter instance here you once you del and you went when the activity is dropped you has for a new instance and then you ask for new instance of factory and then activity one under smoo stuff that we can have also is that coin bring some cool stuff for the view model for the view model API that mean that we bring extensions to make your view model dependency injection quite natural that mean that just now you can think about constructor injection directly in your in your view model class and here the view model is the base view model from the Android activity and Android API and in the module we just have to use the keyword view model to declare the components and that's it then we just need to resolve anything that may need to be resolved in the in the constructor in the activity instead of using by injects I will use by view model because I have to ask Android to make to subscribe your view model instance to the factory and we make it for you and it's freedom just one line and you have constructor injection for your i'm activity and fragment with the view model for the fragment we can also have something interesting that is you can share directly you are instance of the view model from the activity to the to the child fragments and it's just done in one line here this is kind of the same api call here the see that we rely on the parent activity instance you will see that also it's clearly easy to test with coin and then it's a good thing that everything that you've write for your app you can easily play with it mark and run some kind of strategy with your module just line here it's the coin test module that will help you make the the testing stuff one of the main stuff that you have to use here is the checking is checking your modules that mean that we are clearly completely dynamic and then if you don't check your module I will say with this mechanism you will have to discover that sometime you will discover a finishing crash on run time and then to prevent from hit we just run the j-unit because finally the there's the the checks of your module is just a function again your modules definition and to run it the best way is to run your definition energy unit check it and that's it you are checking all your definitions in one line and you have it on this is your CI and that's okay how do you team is have a secure belt building with that we also have some bunch of extensions to help you with the J unit and we have some point we start coin and we have the contest interface that is a kind of coin component interface but will bring more feature oriented for the testing and here in your J unit you will directly use any kind of component that you declare in your modules directly like that it's very present to use that and also you can declare on the fly any market for any kind of component and the any you can do anything you want then because you will resolve your component as a mark and not has the original component here from that okay then now I will talk about coin to zero okay and actually the the the the current version you can stable version is 10.2 and since the first version we are trying to keep very focused on something to keep a coin really really I would say simple that mean that from the beginning the target was to keep the GSL as simple as that that means for keywords okay that mean that anything anyone that read coin configuration file clearly understand what he does and don't have to say okay this is a mechanism that do this and have to win for this no we just write a stuff it's explicit and you don't you just have four keywords for that the other target of coin is keeping the pragmatic API that mean that we just focus on what global people need about this definition and we still are very focused on the gate and inject function that mean that mainly all you will do all you will do is retrieving instance by get and inject okay and sometimes when you have miss I would say that if you miss somewhere the gate or inject extension you will use the coin component interfacing tag this class and have access to coin API with that and that's it that was the first table version of coin but finally there were some contributor that ask us about benchmark and he was benchmarking coin with 540 definitions and then the benchmark was not very good because the air injecting definition and we truly knew stuff was around 140 milliseconds that's quite good and then I decided to rewrite all the internals because then we were real I the internals were using heavily functional code cleaner and the logging engine was not so good then finally after that I began to rewrite all the internals keeping the same API and having a better benchmark here result that mean that that with the definition graph of 450 definitions when you resolve a bunch of definitions you just resolve it in one millisecond some cool these other stuff that can be interesting here is that we can compare the two version of the coffeemaker stuff here is that about dependency injection and I would say benchmark running against those two those two code okay to see the same stuff we will resume the same kind of components but one side with coin and one side with dagger okay and then what we want to do here is that it's it's an app that is we run that to avoid a cold start with a JVM that we have some kind of overhead into 2 into 2 version we benchmarked and stuff on the odd version that is that we already run the app like in the benchmark from slow to benchmark from slow running run the app ten times and here we just find it happen two times just run this stuff okay that mean that we run we drop everything and we run and then we don't have the starting cost of the JVM it's time and then we have those results that mean that here and you can replay this on your laptop and that mean that clearly dagger brings something that is at start they generate everything for you but clearly in terms of resolution we are very close to the dagger performance for you and there is no magic we have to read the definitions and prepare everything for you to resolve the stuff but this is still something that we can admit in some of dependency and still with I would say cool start and a bit of logging you see that the three definitions are resolved read in value of one milliseconds and if we try to run for Android definitions I'm around 12 years ago not so bad and then be aware that these have to be taken also with your device you with all the version of your hardware and stuff because we have to cut lean overhead that can play against us sometime but it's not so much costly if you have any difficulty to play with any difficulty to run modules and start but I think that the worst case is not here you will have to do need of performance when you resolve your definitions if you need to to have better starting process just then speed your module at start and run and load modules when you need it you can have access to those project and the benchmark here and yeah also thanks Floyd to having bootstrap do stuff and compare also all the benchmark like Corinne and also stuff I really into coin to zero okay then it won't be so much difficult because you have just have mainly to rewrite your start coin function instead of in the first conversion stable of the first stable version of coin we provided I would say one start current function pair runtime and we do another thing that we provide one way of starting coin but we provide now in coin to zero I would say extensions of the way of starting coin okay doesn't mean that here in Android don't forget to use the Android context functions to reference your on read context okay and that's pretty much it think that mainly we have merged some package from the standalone to core and when you update from coin one zero two can't point to zero your module your existing module you will just have to make some reimpose mainly and that's it the DSA the API are stay the same but we have some API breakage of course we have simplified module DSL in the previous version we all load some kind of visibility rules and inner module stuff with visibility access and then we don't want to maintain this anymore because this is something that mainly people don't use and make the resolution of the components clearly more complex to do and then for to have such such benchmark now we had to make some compromise also now we rely more on qualifiers and make some default definition resolution that mean that if you have a first definition with the type and you have another definition with the same type you have to make the second definition with the qualifier but the first one will be your default definition in the old version of corn you you will have you to make two qualifiers to have this resolution the scope API is something that is has been developed at the end of the coin 1-0 version and that was something that were hard to bring to maturity now with the new engine we are making a way to write a scope and we try to make simple usage about scope and the last stuff that I would like to say about coin disease now oriented to let you also develop any app in with some kind of isolation that means that you can write any framework or any library and just have your internal instance of coin and don't be polluted by any usage of corn outside of your library if you sorry if you need any any clues about the upgrade there's this reference a great coin that redirect you to the official coin website okay my remote control is not working there's so much topics I would like to share with you and there's so much stuff that I would like to talk about but this is mainly the stuff that has been improved in coin to zero and we just we will just take a look at the scope API and how we can bring a sister for you yeah just like take a look at the scope API together and okay what what is a scope then a scope it's just the context it's it's something that hold your definition for fixed duration of time and when you finished with this result definition you just want to throw them away and you can inject those instance anyway the best thing is to try to have some kind of allergy analogy to component lifetime and the the longest lifetime that you can have in your components is tied to the container and the container is tied to those single definitions they are never dropped they will survive until you have to stop your your coin container when you use factory it's some kind of snapshot and one use one one one time use of component instance and we need some kind of interment intermediate usage some kind of component that are not singer but component that you want to throw away at a given time the best way to think about that is to think about boxes okay think that you will bring boxes for a given time and you have to think if these boxes you want to show it directly if you want to keep it for a certain time that need that every singer you keep the box the factory you just fold the box away when you have to use it and the scope you bring everything in you want in your scope box and then when you are finished with your scope you just throw away this box and to give you more I would say so to be to be more close to something that you are perhaps they will be with single toys you can have some repository or comprehend that surviving for the for a long time but for the scope you will need it for for example user sessions that mean that you have to register user related data for a given time and when you log out you just throw this away then how we write that then in the module okay we have to open a scope structure definition that is cope function and we have to name with the qualifier he is the qualifier function that answer or you to give a name hyzer with the string either with the type that allow you to use strings if you want to use more strong stuff then here we are detailed we are detailing a cessation structure and we will have scope definition inside that mean that we have a structure of the box the session box type okay and we have a session data definition inside and how we will use that in that here we want to create an instance of this box here this name is session 1 we give an ID and we we say ok I have my box session 1 and what I want to do is just get from Dick's box this box any dependency instance that I need and what I've done okay close it here a good some point goodness for you to help you deal with scope API day by day on the Android side is the coin Android scope dependency this dependency is included with the cone enroute view model dependency and what these stuff frames is that it brings extension function oriented for Android lifecycle to bring you to bridge with you for the coin scope API in clear we will make a scope foreign activity and then mean that here I make a structure of scope for my activity here and clearly I want to have some scope component like a presenter or other stuff that can survive for the duration of the lifecycle of my activity and then how I will use that in my activity I will have a current scope extension property that will be easy bring magically by cohn here and this current scope it's a scope that is automatically created at the beginning of your activity and drop at the end of the life cycle and that you don't have to do anything the scope is created for you you just have to dig into the definitions to have that unit here and that's it yeah just to finish that finally the current scope is in extensions of the API I hope to learn interesting thing and finally to give some takeaways for the newcomers coin before starting coin okay what you can do is that you can try the gating starting project and this cuttings a getting started project is a bunch of Ghita project that you can just clone use this very small example and then you can play with android android model on which go up and review model and its little program to allow you to play easily with very small api we have quick references online to have i would say a quick answer for you about the EP but about the daily usage of coin and after that my advice is that coin is oriented to be simple right simple things with that and make it step by step I mean that try to make simple components right it step by step and check your modules okay it's a good good habit to have and because this is the safety net for you and you will have some bunch of modules and bunch of definitions don't be afraid to use the check module definition one of the best feedback that we have on coin is that people saying that finally they discover that they can write things seem in a simple way and they also review their their architecture their components and they make me carry in a more easy way as before and this is the main the main message keep everything simple as possible and also with the cop don't try to use directly the scope API try to challenge yourself about about the lifetime of your components do my app is just running it with all the stuff all the time and you can just start and stop and restart coin or just have to have complex tgd whisk up and have then custom stop oriented dependency injection we have documentation reference hard to maintain I think this is the hardest part of the open-source project is to keep documentation reference up-to-date but you can contribute and you can help us maintain all the stuff you can have the reference of everything the api javadoc everything and all is on github from the website documentation to all the documentation and all the project and if anything goes wrong you can directly go on stackoverflow github and slack and on slack we are on the cutting slack instance and you we on the coin channel easy to find you can follow us on twitter we can also have the coin developer channel on medium that publish all the changelog and some main articles about people and the main stuff is that carry it's important to have your feedback and now coin has been starting in 2017 the project which is now reaching three thousand four hundred thousand it emits bright cool and then i want to thank all the early adopter all the first people that believe in the project i continue to contribute and continue and have the community to to growth one last takeaway for you is the website inside Kondo tayo and that's it for me thank you very much [Applause] how much time do we have perhaps one minute oh I can I will be outside also to answer any question about sorry testing yeah no no the the API is do the same and mainly the point two zero bring new internal in new internal stuff and you use coop and use in the context insulation yeah first yeah just use the coin call the coin codependency yeah I think that [Music] depending it's not something you I think that you won't survive to screen rotation like view model it's a kind of intermediate view model stuff that you that you can make with the presenter because I think that you destroyed the activity and then we have on destroy event and then because we have the on this tour event we just close the scope for you but if you don't if you don't if you just apply a new activity behind your activity then you don't close the scope because the you are tivity is not is not closed yeah one last yeah now nautical and not at compile time just on runtime and if you use the check modules you will have an error on here saying that you forget to provide something with this type okay thank you very much [Applause]
Info
Channel: Android Makers
Views: 15,104
Rating: 4.9468436 out of 5
Keywords:
Id: chCsNkjotfc
Channel Id: undefined
Length: 43min 14sec (2594 seconds)
Published: Sun May 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.