Dependency Injection Patterns

Choosing the right pattern for implementing dependency injection is an important task and can affect your class’s usability and functionality. In this post I’ll overview 3 patterns of implementing dependency injection - constructor injection, property injection, builder (Joshua Bloch’s pattern, not GoF pattern). For demonstration purposes we will work with a class called TextTranslator, that requires 3 dependencies: TextReader, TranslationService and TextWriter:

public class TextTranslator
    protected TextReader textReader;
    protected TranslationService translationService;
    protected TextWriter textWriter;    

The sample code will be written in C#, but the examples are applicable to Java and other object oriented languages.

[Read More]