Aug
19

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

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

  • acrylic pain brushes February 5, 2016 at 3:16 am

    Hurrah, that’s what I was searching for, what a information! present here at
    this webpage, thanks admin of this web site.

  • szyldy reklamowe tarnów| szyldy dla firm tarnóW February 5, 2016 at 2:06 pm

    Bardzo dobry wpis, bardzo długo szukałam informacji na ten temat,
    a tu w jednym miejscu znalazłem odpowiedzi na wszystkie dręczące mnie pytania.
    Bardzo mądrze
    i szczegółowo omówione zagadnienia. Szczerze polecamwszystkim z podobnym problemem.

  • Http://lifestir.net/ February 6, 2016 at 2:31 am

    La Red de la Comunidad de TIC, Centros de Contacto, CRM, Social Media y Marketing te da
    la bienvenida al blog de Mundo Contact.

  • http://beknown.com/kevin-shute February 6, 2016 at 11:39 am

    People link together to perform various roles
    in their lives such as a husband or a better
    half in your home, an employee or a company at work, a colleague or a buddies to a person, an active or a passive person in public areas.

    My website http://beknown.com/kevin-shute

  • Blondell February 7, 2016 at 2:14 am

    Only beneficial information is provided by this website and
    I am checking to it this instant! Thankyou people!

  • vastu compass February 7, 2016 at 10:33 pm

    You should take part in a contest for one of the highest
    quality blogs on the web. I will highly recommend this
    site!

  • Levi February 8, 2016 at 7:43 pm

    Gorgeous! That you do not come by info like this simply and I am gracious!
    Maintain it up guys!

Post comment