 A hand for Alex. So, hi everyone. We've got my breath press, we're also hiring, so. If you see this guy in a red t-shirt, or you or anybody from Brown, just do it now. So, today we're going to talk about how to be able to compile time safe and eventually with a needle. So, a needle is an open source product from Uber. So, we're going to talk about it today. So, my name is Alex. I'm an ice engineer at Grapp. A little bit of history for me is I was on holiday in Singapore and I attended this meetup mainly by accident. And I met some guys from Grapp, our general management program and a previous mobile engineering lead that invited me to Grapp and I'm staying two years there. So, let's talk about the dependency index. So, there are going to be three things I'm going to talk about. First is what is dependency injection and why we should be using it, why it's helpful for building a clean and building a better apps. Secondly, we will talk about Intel DiSystem, which is intended to help us reduce some of the boilerplate call-out that we should write and help us writing the code faster. And we'll have a small demo. So, what is dependency injection and why should we use it? So, as an article on Wikipedia it says dependency injection is a technique whereby one object supplies dependencies from another object, which is a fancy thing to do to say, a fancy way to say that just provides some stuff in the initializer or injects through the setter. So, there are three components in dependency injection. One is dependency or the service that we need to inject. Another part of dependency injection is a client that is using the service or dependency. And then there's an injector or basically it's the place where we provide service to the client. So, what does it mean in practice? Let's look at a pretty common example. I'm writing my project for the first time and I'm again going to do just a URL session chart that it asks with URL, do all things at all places. And it's that for several reasons. First is, it's pretty hard to test because I'm writing, I'm hard coding my URL request, I need to be controlled. So, there is no way for me to simply write some logs and provide some data in my test selection. So, I can check that my class is working as I expect. Second is, if I take a poll to this using backend, what if I want to get data from, for example, core data or any local storage or shared preferences or any other place. So, how we fix it? So, first we need to do a couple abstract art, getting data from backend with something simple, so something like that will hide the implementation details. So, we'll create a protocol that will give like a simple idea. So, we can probably say, hey Mike, here's my post ID. This is a lot of the post details and I want to get a callback when you get all the information. So, second we'll move the code that we initially created to the new controller, move it to the service. So, we'll create a class that adopts this protocol, we will move the implementation to this class. So, it's no longer released in the context of our new controller. It shouldn't, new controller shouldn't be aware of how implementation looks like. So, third we need to update initializer. So, we can also fade and inject the service by center, but in this way, we can have no actual type of the service so we'll always know it's there. It's not gone and let's think to check. So, we'll initialize our new controller with the post service, which is no longer concrete type, it's no longer some data task or I don't know where the data is coming from. So, next we need to inject the service to the client basically just pass the internet and instantiate the post service and pass the concrete implementation by protocol. So, next we can clean our code. We can remove the previous implementation and use the service that will provide the clean API to get my post ID so I can receive a callback and I can check all of this and error or I get a post and I can show my details or I can show an error if that's the case. So, that gives me an advantage of actually writing a pretty clean unit test so I can create a simple mock implementation that I can pre-fine my expectation I would say here is my result. I can also keep track of how many times the data being loaded I can also, so here is not shown but I also can track what post IDs are being passed to the service. So, next I can write my unit test, I can instantiate the post and set it to the mock implementation and then when I call the unit load I expect I expect my service to be called and then I expect to I said that my post to starting my post will be will be returned so I also can check now in a synchronous way that my data is set up as per my expectations. So, as shown in that post it improves the stability so I can have a mock that can work seamlessly so I don't have to set up expectations and wait for a few seconds and hope that I won't have flight to test in my setup. So, it provides me with this company whenever I want to replace implementation with something else so here is it was loading data from back end but it might as well be loading data based on just the mock implementation as an example and also it helps moving unnecessary logic so when you think about removing logic from one place to another you make your unit load cleaner you focus more on what your unit load or logic model or whatever architecture you're using so your module only focuses on stuff that matters at this point. So, what should I inject? Actually the list can be endless so pretty much anything that is being used by the client whether it's just a simple feature flags or user settings or calls to the API could be injected So, how can needle help us with that? So, as I mentioned it's open source product number so there are main features of needle it's a compile time save which actually means that if it got compiled then it means that it works so you will not have any unexpected behavior in runtime we won't have any pressure so there is the guarantee to be where it needs to be it also has a pretty performance so it wouldn't enlarge the basis it works pretty fast so because it's large in quad generation it can provide the compile time save and the way it generates works pretty fast so it's also embeddable with any architecture or you can also use needle for backend projects or mac apps so it's actually architecture and platform logistics so whatever projects you have so you can use needle and also it encourages peripheral structure so for example if you have imagine your app is a tree of nodes or tree of features and essentially it invokes creating another one and basically it's a tree or you can think of a tree so each node can introduce some other new components or new service that can be provided down the tree level so it's actually the base of two parts first part is a needle foundation it's a series framework that provides base classes, interfaces protocols that we will use later when we can start with an C3 sorry and second one is a command line tool that actually generates so it requires all things up so to set up it's pretty simple you can install a needle command line tool through a brew so second you will need to add needle foundation framework to your project so needle foundation supports all popular package renters you can use CocoaBot you can use FireTouch or you can use SpeechPack so when you start when everything is set up you need to round generator that's simple interface you needle generate then you pass the path to the generator file, the file which will have all generator plots in one place and you also need to pass the path to your source plots which will reversely needle will go through reversely the whole series files and look for components and construct construct so after you if you have an empty project and you go ahead and generate first generate the file it will have to it will just have one function that you would have to call in your app delegate so here's where you register your provider factories that will that will help you in your advanced dimension so let's start first you need to add the initial component component as a dependency container that will hold all your dependencies that you would have to pass to your children that will consume them so for root component you need to use so the interfaces that follow you each component should be inherited from component class which has a generic sort of associated with for first one we don't have any dependencies so we use empty dependency protocol finding value of foundation as well as we need to create each component to be created for the parent and of course for first forward component we don't have any parent so we use the system empty components that would start with certain components so now again component acts as a builder if you want you can have a builder separately but in this case I'm just using the component as my builder so I can construct root view controller and I will update my app delegates with root component which will get the root view controller and stop instantiated for from now the process of setting dependencies will be the following for your component you will declare dependency then you will run middle generator and it will automatically create all the services for you so for example I have I create a new component which is a home so I have a home view controller that I want to provide a loader start state so I create a home dependency which should be very dependency critical and then I basically write a requirement I want to get loader service for my parent so if I and then I can declare I will start my home component in my building the parent I create a home component with parent which is self in this case it's a root component and then I run generator again so because the way it works is you declare dependency and then it looks up the tree to the nearest parent which has the same name and the type and it will generate the quote and wire things up so because I haven't created any component in the root in the tree right now doesn't contain this dependency it will show you an error which you will see like ok there isn't the same loader service with the type loader service so I can go ahead to the root root component and create this dependency so because the type and name should match 100% so I should write it in the same way and then I create the object which confirms the loader service protocol here I'm using shared keyword which is basically a keyword that will return in this case it will return just once so for example we have multiple children using the same service only one instance of that service will be created you can remove the share for each child that will use the service the new instance will be created so it's up for you to decide whether you want to have one instance or multiple ones so now I can in my home component I can create my home view controller now since I have my dependency in the dependency protocol I can take it from dependency and pass it to my view controller so now the home view controller can use it inside so let's see a small demo so I did set up my my dependency free so here is how a generic example looks like so here I can see that I have home dependency and it gets the loader service from the root component which is a nearest nearest parent in the tree so if I look at it and remove all that okay sorry I call meable generator and then so it's generated again so I'll close compiles so this project is a simple track record for example a simple expense record that I will use well that's good I think you can do it best so actually I use the noodle for my project and I'm pretty hate there are about 30 few places that I can I can fix to make one more tree alright let's go so here I have a loaded form with some information I don't need to use or text okay here I have my transaction screen and my profile and here I see the email that I'm loading and I have my name that was a little bit from back then but if I go to profile you can like I see that my user is actually is actually returning and get it from the shared instance which is a global single tone which is actually a bad practice tone I had and just move it because I should do it so here I'm going to declare user property that should be should be received from single tone so I go to my profile and have a complaint declare it for it so I declare in my profile dependency that I want the user to be passed from the parent so now I need to fetch it to my controller or initializer so now I see an error something is missing so we go back and generate stuff again oh sorry that's too small so I didn't receive any error that means that somebody in the dependency already declared this value so now I can go and check because I have the user already and I am not taking it from single tone so if I run my app I expect everything to work the same way as it did before so I'm going to my transaction and go to the file and yes I see the email here so that means the value most passed down the fans entry thanks any questions regarding the rule does video support scope injections scope injections like if I have multiple modules that I want some dependencies to be shared in certain places but not in others so naturally from some not this dependency will be available so by default every child starting from that not will be able to access it the way you can ban clients from using it and maybe so first but by default like each not in the tree will be able to access the dependency but like not on the same level will be able to do so so there are the dependencies can either be a single or created every time it is accessed or so that's up to you to decide if I have shown you one of the examples you have this shared so this is a keyword shared which means that in this case the water service will be created when it wants so if you rule that and just return water so you just construct so water in this case without share it will be created every time it is subjected as a dependency to another new control or whatever so let's say I have three businesses each that then have a different flow and then I want to have three URL sessions with let's say business one the views in business one will share the same URL session and all the views in business two will have another URL session and all the views in business three will have them the last URL session that means that because let's say for each URL session I want to have a different cache or a different timeout any kind of different configuration so at the point where you can start business A you basically should create another URL session so let's say when I start business A I would have the business A current control yes for us unshoutable you mentioned that we don't have the hierarchical view of our application you also showed us the preview where we have login and home scope but most of the applications are never in a tree structure it's always a complex problem because some of the view control is maybe pushed from the two other places so what happens if a view controller have material parents how in that case the different BI will be able to have a chance to view them so for example if you view control or some screen was created in one hole and then parent if it was closed produced as dependency so and if you want to instantiate it from another place where you can go out of the tree structure you will not find it that means it will generate an error basically because it will not find dependency out of the tree so basically you can in the parallel law you phrase another instance of the dependency or you move the creation of the dependency up to the upper level which is a shared parent for both both those but then I have to repeat suppose if it's a on the fifth level I have two components which wants to call each other but they are also called from the different parents then I have to so view control which can be showed from different screen and you have to repair some problems to communicate to those two parent like common parent here is one you shouldn't patient access to the tree gate so it's always from the top down create another branch of the tree to make sure that it is thank you thank you thank you I guess that's it