Jeff's Series

Transcript

Tap on time to skip ahead

00:10

00:10

I want to talk now about two different approaches to injecting dependencies into a class. The first is call Property Injection. It is the approach that you will use when you typically can’t or don’t control how a class is instantiated. For the app delegate, there’s already a designated way of instantiating the app delegate and give that instance, you would inject its dependencies via property injection. Here’s the example.

00:44

00:44

Inside of the assembly, the application assembly for app delegate, I declare the app delegate class and then I use the injectProperty method on the definition, and I simply reference each of the properties that are injected and then where they come from. Here, the app delegate needs the bundleIdentifier and that dependency is resolved from the coreComponents assembly in a method called bundleIdentifier.

01:19

01:19

If we look at that real quick inside core, inside bundle identifier, we can see here that this comes from the mainBundle and it executes a method called bundleIdentifier on the mainBundle. The mainBundle is declared down here. Which simply- the way it gets a reference to that is that it uses NSBundle mainBundle.

01:49

01:49

Heading back to the application assembly, we can see that the application delegate- the injections are all property based injections. ContentResolver, dataDragonSyncService, an array of loggers, NSNotificationCenter, mainScreen and window. This is an example of property injection.

02:14

02:14

You also typically use property injection when you are injecting into view controllers. These are all declared on my storyboard and there are designated ways of instantiating those classes. Instead of using that, it will get the instance and then inject using the property. Here for this view controller, it has four dependencies, the contentResolver, the bundle, the device and the factory, and then the Champion Skin View Controller has two dependencies.

02:45

02:45

If we have a look at the app delegate class, you can see all of the dependencies are declared as properties and they correspond to the configuration in application assembly. The bundleIdentifier, the contentResolver, the dataDragon service, loggers, mainScreen, notification center and window. If we go back here, again, it corresponds with all the properties. These can be listed in any order. Typhoon will inject all of it’s dependencies via the properties.

03:18

03:18

The second approach to injecting dependencies is actually called Initializer Injector. It’s also, you may have heard it referred to as Constructor Injector. That’s where all of the dependencies are injected via the initializer. The best example of that is down here in dataDragon. Let me pull that up.

03:43

03:43

For example, let me use one of these here. The dataDragonSyncService. This is a class that uses initializer injection. We’re going to instantiate an instance of the dataDragonSyncService with this initializer, with contentResolver, with taskFactory. That is an initializer function that I have written, all of it’s dependencies will be pass as arguments to that function. Here we use a little bit different syntax, we say “initializer injectParameterWith” and then the dependency that we want to pass along. These have to be specified in the order that the arguments are declared on the initializer function. We have inject with parameter contentResolver, which comes from the coreComponents assembly. Also, it needs a taskFactory which comes form the coreComponents assembly as well.

04:51

04:51

We can have a look here at the corresponding dataDragonSyncService declaration and you can see here’s the initializer function with the two dependencies declared.