Dependency Injection – Keep your privates to yourself

Author Eli Lopian    Category .NET Tests, TDD     Tags

Jacob Proffitt has hit the nail on the head with his “I do wish that people would admit that DI doesn’t have compelling applicability outside of Unit Testing” post, the discussion continues with Nate Kohari response post and Jacobs counter post. Oren has also joined in with two posts.

Here a some ideas for you to think about.

DI as a Silver Bullet – You lose power

When you use DI as a ’Silver Bullet’ you are losing more then half the abilities of your programming language. You can not use static methods or the ‘new‘ keyword or sealed types. Oh, you have to make all your methods virtual too. This is going to be even harder once Extension Methods (and Linq) become main stream.

Code to Enable Change – Costs More

Creating Code that is there only to “Enable Change” has a big “YAGNI” written all over it. (Making the code more complex and thus wasting time in development and then in maintenance, for a chance that it will be used in the future). We have understood that this is the wrong plan ages ago. We now know that we can not predict the future and it is best to change our code only when we need to (a process called refactoring).

Keep your Privates to Yourself

Jacob, you are quite right about DI coming into fashion only because the tools where not good enough. DI does have other applications outside testing, but it is being overused in most application only to make the code testable.

One of the first issues that was discussed when TDD was starting was: “should we change our code to make it testable?”
Should we change the visibility of our code?
Should we change the design of our code?
This lead to a collision between testable code and OO encapsulation. Developers started to expose private parts of the code to enable testing. It started with private fields and methods and now it is the complete design. This collision still exists when using inadequate tools, but with the right tools, you can enjoy the best of both worlds.

Get the Best Tools to get the Job Done

Thousands of developers have understood this and have started testing there code without being forced to use a specific pattern required by some tools. Many development shops, didn’t test their code because of the testability/design collision. With the right tools they now understand and test their code.

As Joel Says: “Get the best tools

Bookmark and Share

53 Comments to “Dependency Injection – Keep your privates to yourself”

  • Raymon November 26, 2015 at 4:44 pm

    Nowadays, the conditions for your website to be ranked high in the search engine results are based on the search engine optimization techniques that you decide to
    use. The trends in this industry change and new developments take place only when Google comes
    up with some novel feature that ends up breaking every site
    (Raymon)’s SEO and compels them to consider a change in their current content presentation in order to do well.
    Depending on how how much time you have you can do this about once
    per week. Eventbrite is an online party-planning tool with KISS (Keep It Simple Stupid) design so it is usually a top rated choice among event planners (organizers).

  • November 26, 2015 at 9:54 pm

    It is not too easy to find information that is important but this definitely qualifies
    together. Leaving can undoubtedly return soon and happy!

  • elite of vegas November 27, 2015 at 8:39 am

    It’s so difficult to find important information today, but this surely
    qualifies together. Making certainly will truly keep coming
    back shortly and content!

Post comment