DEPENDENCY INJECTION FRAMEWORKS

If you think of your application as a puzzle, dependency injection (DI) frameworks are the tools that put these puzzle pieces together. Some might even say that dependency injection frameworks are just an implementation of a super factory design pattern. Chapter 5 discussed the basics of dependency injection. The examples used Ninject, but Ninject is not the only dependency injection framework on the market.

In the past, most dependency injection frameworks were configured by using XML files that soon became difficult to maintain. Most modern dependency injection frameworks still support this method of configuration, but this method is generally frowned on and has been traded in for cleaner approaches. This section explores a few alternative dependency injection frameworks using the example from Chapter 5 to inject dependencies into a business application.

When choosing a dependency injection framework, you should follow two rules:

  • Keep the framework at a distance.
  • Focus on the Inversion of Control pattern.

Structure Map

Structure Map is an open source container framework that has a fluent API that makes the code easy to read and maintain. One of the more powerful features of Structure Map is the automocking container.

The Structure Map automocking feature allows you to create stubs automatically when requested by the test. Even though you still need to set your expectations on the mocks, this saves time when you create mocks in your tests.

The ...

Get Professional Test-Driven Development with C#: Developing Real World Applications with TDD now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.