This workshop will be retired on May 31, 2020.
Guidelines for Dependency Injection2:45 with Amit Bijlani
A few guidelines on when to implement dependency injection in your projects.
Is Testability important to you? If so, then it is essential to identify external dependencies within the class that you wish to test Once dependencies can be injected we can easily replace real services for mock ones to make it easy to testing easy.
There are two type of classes: Basic and Complex. Basic classes are ones which have no dependencies, or which depend only on other basic classes. Many of those within the standard library would be considered basic such as: String, Array and Dictionary.
Complex classes are the opposite. They have other complex dependencies, include application-level logic, or access external resources such as the disk or the network. Most of the classes in your application will be complex, including almost any controller object and most model objects. The easiest way to get started is to pick a complex class in your application and look for places where you initialize other complex objects within that class.
If an object is creating instances of other objects that are shared dependencies within other objects then it is a good candidate for a dependency injection.
Now that you understand what is dependency injection and 0:00 how you can implement it, well the question still remains, 0:03 when is a good time for dependency injection in your projects? 0:08 Well let's outline a few guidelines that you can follow. 0:12 The first one, is testability important to you? 0:16 If you're a responsible developer, then testing should be a priority. 0:20 And if so, then it is essential to identifying external dependencies within 0:24 the class that you wish to test, and 0:29 injecting those dependencies using any of the methods previously discussed. 0:31 Of course, constructor injection being the safest method. 0:36 Once dependencies can be injected, we can easily replace real services for 0:41 mock ones to make it easy for testing. 0:45 Well, the types of classes to inject, 0:49 there are two types of classes, basic and complex. 0:52 Basic classes are ones which have no dependencies, 0:56 or which depend only on other basic classes. 0:59 Basic classes are highly unlikely to be subclassed because their functionality is 1:03 clear and variable, and doesn't reference external resources. 1:08 Many of those within the standard library would be considered 1:14 basic such as string, array and dictionary. 1:19 Complex classes are the opposite. 1:22 They have other complex dependencies including application level logic, 1:26 which may need to change, or access external resources such as the disk, 1:31 the network or global and memory service. 1:37 Most of the classes in your application will be complex including 1:41 almost any controller object and most model objects. 1:46 Many foundation classes are complex as well, 1:52 such as NSURL session, UI View, UI View controller. 1:55 Now given these classifications, 2:00 the easiest way to get started is to pick a complex class in your application, 2:03 and look for places where you initialize other complex objects within that class. 2:08 And finally rule three, shared dependencies. 2:14 If an object is creating instances of other objects which are shared within 2:17 your application, then it is a good candidate for a dependency injection. 2:22 And finally, the obvious benefit of dependency injection is 2:29 a clear declaration of dependencies. 2:33 It becomes obvious what an object needs in order to operate, and 2:36 dangerous hidden dependencies, like globals, will disappear. 2:40
You need to sign up for Treehouse in order to download course files.Sign up