Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
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
how you can implement it, well the question still remains,
when is a good time for dependency injection in your projects?
Well let's outline a few guidelines that you can follow.
The first one, is testability important to you?
If you're a responsible developer, then testing should be a priority.
And if so, then it is essential to identifying external dependencies within
the class that you wish to test, and
injecting those dependencies using any of the methods previously discussed.
Of course, constructor injection being the safest method.
Once dependencies can be injected, we can easily replace real services for
mock ones to make it easy for testing.
Well, the types of classes to inject,
there are two types of classes, basic and complex.
Basic classes are ones which have no dependencies,
or which depend only on other basic classes.
Basic classes are highly unlikely to be subclassed because their functionality is
clear and variable, and doesn't reference external resources.
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 including application level logic,
which may need to change, or access external resources such as the disk,
the network or global and memory service.
Most of the classes in your application will be complex including
almost any controller object and most model objects.
Many foundation classes are complex as well,
such as NSURL session, UI View, UI View controller.
Now given these classifications,
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.
And finally rule three, shared dependencies.
If an object is creating instances of other objects which are shared within
your application, then it is a good candidate for a dependency injection.
And finally, the obvious benefit of dependency injection is
a clear declaration of dependencies.
It becomes obvious what an object needs in order to operate, and
dangerous hidden dependencies, like globals, will disappear.
You need to sign up for Treehouse in order to download course files.Sign up