Guidelines for Dependency Injection

Guidelines

Rule 1.

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.

Rules 2.

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.

Rules 3

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.

0:00

Now that you understand what
is dependency injection and

0:03

how you can implement it,
well the question still remains,

0:08

when is a good time for
dependency injection in your projects?

0:12

Well let's outline a few
guidelines that you can follow.

0:16

The first one,
is testability important to you?

0:20

If you're a responsible developer,
then testing should be a priority.

0:24

And if so, then it is essential to
identifying external dependencies within

0:29

the class that you wish to test, and

0:31

injecting those dependencies using any
of the methods previously discussed.

0:36

Of course, constructor injection
being the safest method.

0:41

Once dependencies can be injected,
we can easily replace real services for

0:45

mock ones to make it easy for testing.

0:49

Well, the types of classes to inject,

0:52

there are two types of classes,
basic and complex.

0:56

Basic classes are ones
which have no dependencies,

0:59

or which depend only on
other basic classes.

1:03

Basic classes are highly unlikely to be
subclassed because their functionality is

1:08

clear and variable, and
doesn't reference external resources.

1:14

Many of those within the standard
library would be considered

1:19

basic such as string,
array and dictionary.

1:22

Complex classes are the opposite.

1:26

They have other complex dependencies
including application level logic,

1:31

which may need to change, or access
external resources such as the disk,

1:37

the network or global and memory service.

1:41

Most of the classes in your
application will be complex including

1:46

almost any controller object and
most model objects.

1:52

Many foundation classes
are complex as well,

1:55

such as NSURL session,
UI View, UI View controller.

2:00

Now given these classifications,

2:03

the easiest way to get started is to pick
a complex class in your application,

2:08

and look for places where you initialize
other complex objects within that class.

2:14

And finally rule three,
shared dependencies.

2:17

If an object is creating instances of
other objects which are shared within

2:22

your application, then it is a good
candidate for a dependency injection.