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

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

  • Dependency Injection - Good for more than just unit testing. : Mostly Progamming Stuff December 9, 2007 at 5:56 am

    [...] Eli Lopian entered the discussion on the negative side of DI but made some statements that I think are a little misleading: [...]

  • 依赖注入是否值得? - 自由、创新、研究、探索…… - 博客园 December 17, 2007 at 2:25 am

    [...] Eli Lopian是设计出TypeMock的公司的CTO也加入争论,他对争论的核心有不同的看法: 当你把DI当作是“银弹”来使用,你就丧失了所用编程语言的一大半能力。你不能用静态方法,不能用“new”关键字,不能用封闭类型。哦,你还要把所有的方法都变成虚拟的。 [...]

  • Poking Bears - Jacob Proffitt January 10, 2008 at 1:51 am

    [...] Poking Bears I can’t believe the potent response I’ve gotten on my posts about Dependency Injection. Ayende Rahien responded individually to each of my posts himself, which is more than a little bit intimidating all on its own and a couple of development heavyweights left comments directly. Nate describes Ayende’s posts as the cavalry arriving and links to a couple of other responses. All of these posts disagree with me, though a post by Aaron Jensen indicates that he’s at least willing to consider the possibilities. All of this should have been foreseeable, really, as soon as I decided to publicly post my skepticism of a technique in the midst of its ascendency just as mature tools are penetrating its development space. Surely something that popular must have some foundation in reality/fact and give measurable real-world benefit, right? The Benefit of Dependency Injection Its boosters see DI as assisting in loose coupling of objects. Anyone around when OOP came in and trounced all comers knows the benefits of loose coupling, right? Darn skippy. Benefits include ease of maintenance and being able to refactor elegantly. The thing is, DI itself doesn’t really do much for loose coupling as I pointed out in my posts. DI alone has little discernable benefit. DI aids loose coupling when you create container objects or a framework to make dependency management and injection easy. It’s essentially an additional layer of abstraction that allows you to create a dependency on the object or framework in place of the actual objects themselves. This is what Ayende means when he talks about a “powerful container”. This is also the key behind Google’s Guice framework for Java and Nate’s own Ninject project as well as the Windsor project mentioned by both Nate and Ayende. See a pattern there? I did. All the heavyweight advocates of DI rely on powerful, relatively invasive tools to realize the benefits of Dependency Injection. Until you get those tools, the benefits of DI seem mainly theoretical. The fact that the addition of a framework or container objects unlocks the benefit makes me wonder if there isn’t a different pattern effectively at work here. Theoretical Benefits Which leaves me where I started, really. You see, context is everything and the context of the vast majority of discussions around Dependency Injection involves using mock objects in unit testing. Bright young developers looking to do unit tests without hitting the database search for tools that will help them accomplish this task. They’ve heard of mock objects and figure that mocking is the key to solving their problem. The trouble is that everywhere they look, mock objects are telling them about “design for testability” and the importance of loose coupling. It’s such a litany that many of them have come to buy into the party line because hey, “it’s just good design.” I’m sorry, but that is bunk. You cannot separate good design from the context and resources of your individual project. If my client or company needs web pages that validate users against Active Directory and then allows them access to reports based on their group memberships, then telling me that I need some robust loose coupling framework in order to test my user maintenance library is flat out wrong. Or take a project like Subtext. I love that project even if it’s been a couple of months since I’ve done anything substantive for it. Telling me that the architecture of Subtext should be crammed into a dependency injection mold just to increase our ability to use mock objects in our unit testing makes me shudder (not that anybody has done anything of the sort, mind. I’m talking theoretically here). In the whole, that’s a lot of overkill in order to extend your test coverage. YAGNI Baby I don’t want to come across as too much of a shill, but I have to agree with Eli Lopian (CTO of TypeMock, so hardly a disinterested party) when he says this has YAGNI written all over it. Have I been involved with projects that took a hit because a dependency had to change? Absolutely. But I’ve also been with projects that took a serious architecture hit they never came even close to needing. Outside of major corporate infrastructure projects, stringent loose coupling just isn’t as much benefit as more naturally encapsulated OO techniques. So here we have a lot of architecture big wigs selling DI as best practices and not bothering to contextualize those statements at all. Again, Ayende is a good example of this. In Dependency Injection: More than a testing seam, he wraps up with “Dependency Injection isn’t (just) for testing, it is to Enable Change.” Nice. So if I don’t use DI, my projects can’t change at all? That’s his implication and he’s not alone. It all comes down to cost vs. benefit and that’s what I’m not seeing enough discussion of, here. What are the costs of using DI? Balance that against how often you really need to change dependency configuration. Add in how hard it would be to go from not using DI to using it and what kinds of triggers you’d use to identify when a project is big enough that the risks of architectural change justify the cost of wide-spread DI. All of these proponents are trying to claim that DI has little or no cost and/or that the benefits are incalculable. I’m sorry, but I’m not taking that bait. Resisting the Hard Sell Frankly, this whole thing puts me in mind of a classic hard sell technique. A customer comes to you wanting widgets. You tell him he can’t have widgets without blodgers but that’s okay because blodgers have so many additional benefits that really they should be coming to you for blodgers anyway. Indeed, all professionals are already using lots of blodgers and they all agree that it’s a good idea to have them and really you can buy yourself some widgets as well! And you’re in luck because we have plenty of the highest quality blodgers in stock right now! So it is with DI and mock objects. I want mock objects so that I can test code without actually traversing the wire and hitting a database. Loose coupling is so far down my project priorities that standard OO practices are more than sufficient. Only, everywhere I go for mock objects, I’m being told that all the best projects are loosely coupled so what kind of incompetent fool am I for not implementing DI even before I ever thought of needing a mock object framework? Fortunately, I grew out of that kind of bullying before I got out of high school. If I ever get into a project where loose coupling is a big enough concern that I have to consider seriously intrusive techniques in order to reduce the risks of dependency change, believe me, I’ll be looking into one of those magic DI containers. There’s some seriously cool mojo there. I just don’t have that need and since I can mock objects without it, I will. On Better Testing A lot of people talk about the benefits of TDD. I tend to agree with a lot of their tenets. I’m not entirely sold on “Test First” (the heart of TDD), but I’m to the point where I’m going to give it enough of a shot to see how much difference it makes and in what direction. Since TDD is such a big deal and considered by most to be extremely important, I wonder why they let themselves be hampered by the DI crowd trying to ride their coattails. The biggest roadblock to ubiquitous testing for me came when I ran into methods that had latent processes embedded in them. This was mostly the case with database access, but also web services or anything else that crossed the network or accessed important global resources. Mock objects were the obvious solution to the problems I ran into. I balked, however, at the degree of change needed to use any of the mock object frameworks I ran across. So I lived with gimped unit tests until I ran across a stronger mocking tool. I can’t be the only one to do this. How much have we set consistent unit testing back by insisting on selling DI as a prerequisite to using our mocking tools? Development Principles Enforcing purity tells people that you’re not as interested in solving their problems as you are in enforcing your code of conduct. “You can have mock objects, but only if you join our priesthood” they seem to say. Again, Ayende is an example of this attitude when he says, “The main weakness of Type Mock is its power, it allow me to take shortcuts that I don’t want to take, I want to get a system with low coupling and high cohesion.” As a personal statement, I guess this is fine. If Ayende doesn’t trust himself to write code appropriate to his requirements and resources, then by all means, he’s free to handcuff himself with less powerful tools. That’s not at all what he means, though. Too often this kind of statement isn’t intended to actually target the speaker. The speaker is perfectly confident in their own abilities—in this case to create low coupling and high cohesion. It’s the abilities of other developers they want to constrain. It’s another manifestation of the “programmers not doing things my way are idiots” meme used against Visual Basic for so long. Personally, I’m a fan of powerful tools that let me do things my way. If there’s something I’m doing wrong, please tell me. If there’s a principle or pattern that you think might be useful to me, I want to hear it. I want to hear the best case you can make and as much of the cost/benefit as you can include. But I’ve been around long enough to know that no principle or pattern is useful in every context so I’m just not going to buy arguments that claim that anything that needs X should also be using Y unless you can show me how Y is a fundamental and indivisible part of X. And I’m sorry but DI is not a fundamental and indivisible part of mock objects. Infinite Cost Arguments I had a post a couple of months ago on my personal blog about safety and infinite cost arguments. Summary: I don’t buy arguments that boil down to one side having an infinite cost any more than I buy arguments that claim one side is free of all cost. This tenet applies to the Dependency Injection equation—loose coupling is a beautiful thing, but the absence of DI isn’t a sign of sure disaster. Frankly, loose coupling isn’t an absolute value anyway, so arguments that every decrease in coupling is automatically worth the cost are absurd on their face. With DI containers and tool frameworks, I’ve come to see that DI can have benefits that aren’t solely mock object/unit testing related. I just don’t think that DI is a fit for every project because the cost is not zero and the alternative cost is not infinite. Frankly, I don’t see DI as a benefit for most projects but then, nobody has bothered to make that argument. As such, I’d appreciate more exploration in the middle and discussions that admit that mock objects that don’t require dependency injection would be a huge boon for widespread penetration of comprehensive unit tests. Or discussions that explain why I’m mistaken in my statements above. A Caution Most developers I know have been on projects where some dependency got spread throughout the code in such a way that any changes in that dependency created a nightmare. I’ve certainly been there. In those situations, the problem was improper encapsulation and the tight binding that brings. Comparisons between DI and those examples are like comparing a bag lady to Ms. America. Yeah, there’s a difference and all, but the fact Ms. America had a manicure isn’t it. If you’re going to make an argument I can buy using comparisons, you’ll need to compare the best of the alternatives to the best of DI. What I’m saying is that while you should put your best argument forward I’m going to balk at straw men put in place of my position. And the fact of the matter is that you can get pretty far achieving loosely coupled architecture before you get to DI. I’m not saying DI can’t do it better, I’m just saying that there are a lot of techniques out there that even medium-sized projects already routinely implement that provide perfectly adequate change protection. DI is on top of, not instead of, those techniques. Technorati tags: Dependency Injection, TypeMock, Mocking, Unit Tests, TDD, YAGNI Posted: Aug 20 2007, 09:10 PM by Jacob | with 7 comment(s) Filed under: Dependency Injection, YAGNI, TypeMock, TDD, Unit Tests, Mocking [...]

  • ekte canada goose jakke September 8, 2014 at 2:15 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian
    ekte canada goose jakke

  • canada goose solaris parka September 8, 2014 at 2:16 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian
    canada goose solaris parka

  • ugg australia st酶vler September 8, 2014 at 2:16 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian UGG b忙kke h酶j
    ugg australia st酶vler酶vler.html

  • find burberry horseferry check crossbody briefcase barrel of bargains September 8, 2014 at 2:17 pm

    The Astonishing Lucrative Power Of buberry
    find burberry horseferry check crossbody briefcase barrel of bargains

  • camo uggs September 8, 2014 at 2:17 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian UGG st酶vler m忙nd
    camo uggs

  • ugg australia denmark September 8, 2014 at 2:18 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian ugg st酶vler i danmark
    ugg australia denmark

  • uGG b酶rn September 8, 2014 at 2:18 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian ugg st酶vler udsalg
    uGG b酶rn酶rn.html

  • hot value burberry digital sport watch 36624881 black on sale online September 8, 2014 at 2:18 pm

    buberry can help every one of us by adding a number of exceptional features and capabilities. Its a unvaluable item for any supporter of buberry.
    hot value burberry digital sport watch 36624881 black on sale online

  • uGG Dakota September 8, 2014 at 2:18 pm

    Dependency Injection – Keep your privates to yourself « Development and Integrity Management by Eli Lopian uGG sandaler
    uGG Dakota

  • sepia uggs clearance November 7, 2014 at 1:17 am

    Hello! I’m at work surfing around your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the fantastic work!
    sepia uggs clearance

  • Cheap Air Max 2014 Black Mens March 18, 2015 at 2:16 am

    This blog won’t show up correctly on my iphone – you may want to try and repair that

  • gjnoxmfr March 18, 2015 at 8:06 am

    FOLLOW THE STAR-LEDGER: | ralph lauren New Zealand and Sri Lanka meet in the opening World Cup match in Christchurch on Feb. 14. z
    An article in the Star the following day confirmed that Jack T. Ryan died of his injuries, but offered nothing new on the feud.聽 michael kors outlet online Thomas, who played the Palmer course, said the wind 鈥渨asn鈥檛 really in a consistent direction the entire day. It was supposed to be northwest or north-northwest and it just kept swirling a lot.鈥?
    In 1984, Bruce Springsteen’s song “Born in the U.S.A.,” written from the point of view of a contemptuous Vietnam veteran in a dead-end world, found itself celebrated for its rousing chorus. coach outlet store online Meryl Streep extends her lead as the most nominated performer with 19, thanks to Rob Marshall s Into the Woods. b gucci
    Everyone knows rice is a stellar side and main锟絙ut it also can lend a quite fantastic flavor to dessert.锟絋his warm pudding staring dried cherries and toasted almonds has a surprisingly velvety texture. PALM HARBOR 聳 The Palm Harbor Chamber of Commerce and residents will honor survivors and casualties of the surprise attack that brought the United States into World War II on Dec. 7.
    p Football europ茅en, mythe ou r茅alit茅 ? gucci outlet online “I make up my own chords and stuff like that but I鈥檝e not really written anything yet. I do go on the Playstation from time to time but find there’s nothing better than a bit of strumming or listening to music to relax. tory burch outlet online
    - Jamarius Henderson finished his high school career on a high note. toms shoes outlet Share
    State police Lt. Richard L. Krawetz, the trial s first witness, testified that when police showed up at the property, Selenski laughed. coach outlet There are high utilizers who are well off and well insured, of course, but many are uninsured or underinsured people who use the emergency department as their sole source of health care. n abercrombie kids
    鈥淎HCCCS鈥?provider payment rates are so low, you don鈥檛 get a lot of private practice groups that want to go work in those areas,鈥?she said. That makes these communities dependent on nonprofit groups and community health centers. burberry outlet online MOPS (Mothers of
    Article published on Tuesday, Dec. 23, 2014 hollister clothing SHOREWOOD INVITATIONAL
    During his five seasons as a head coach, Mangini s teams never had a defense that ranked among the top 15 in terms of yards allowed. His 2006 Jets ranked 20th in yards allowed while giving up the sixth-fewest points. Mangini went 23-26 with the Jets, 10-22 with the Browns. tory burch outlet Medjool Dates: One of the sweetest fruits on the planet, tender Medjool dates are available in most super- markets now.
    It didn’t help the Quakes to lose their leading scorer for the World Cup this summer. The simple fact is San Jose has not had a consistent starting lineup because of ill-timed injuries. The Quakes entered an unforgiving stretch missing four of their best offensive players and U.S. national team defender Clarence Goodson. oakley 鈥淟isten to fast music if you like to be pumped up, or calm music if you are getting too nervous and you want to calm down.I do listen to music, probably more than I used to. When I started running I鈥檇 have to carry a WalkMan, but now it鈥檚 a bit easier.
    n rebounds against Tennessee on Feb. 20, and 15 points and nine rebounds against polo The much awaited census of Northern Cyprus will start on 4th December. The census was supposed to be conducted in October. The census will structured by the State Planning organization.
    Twitter’s challenge brings that battle to the modern era, allowing the court to decide “whether government’s need for secrecy survives a First Amendment review,” Opsahl said. af Democrat: 35% l
    Friday’s announcement is expected to help the company and the universities as they attempt to recruit a Johnson Controls-endowed energy storage professor at UW-Milwaukee and Madison. toms shoes outlet “That was a championship game,” said Seahawks coach Pete Carroll.
    b Fees: Free. lv bags Follow Craig Gilbert on Twitter x
    鈥?Use a broker: Just as you would use a real estate broker or agent to guide you through the purchase of a home, consider relying on a franchise broker when buying a franchise. A broker can guide you through the selection process and can help you avoid certain risks.聽 As an expert in franchise documents, he or she can save you considerable time and confusion. michael kors outlet (Inman)
    ciBjb25kaXRpb25lcnMgYW5kIGJvZHkgd2FzaGVzLiBUaGV5IGNhbiBjb250YWluIHByZXNlcnZh coach outlet online I didn’t necessarily have the desire to do it full time, said Lause, who graduated with a degree in chemistry and a minor in business. Once I started to get competition online, I got this entrepreneur blood going. m michael kors
    A similar struggle will also be gong on in Congress. Sen. Dianne Feinstein announced after the Newtown massacre that she would introduce a bill to impose a new assault-weapons ban, and . However, gun-rights groups are mobilizing their supporters to fight the bill. coach outlet online Eroglu and Christofias will together attend the opening ceremony of the Yesilirmak crossing point in the northeast of Cyprus.North Cyprus leader and South Cyprus leader together will open a new passage point in the northwest of the Cyprus.The link is named as Yesilirmak crossing point which will connect parts of TRNC and Greek Cyprus.United Nations Peace Keeping Force (UNFICYP) said that the opening ceremony of this passage will take place on 14th October.Turkish Cypriot President Dervis Eroglu, Greek Cypriot leader Demetris Christofias, EU’s Enlargement Commissioner Stefan Fuele, Special Adviser to UN Secretary-General on Cyprus Alexander Downer and Frank Urbancic, the U.S. ambassador in the island will take part in the opening ceremony of the crossing point.Eroglu and Christofias will give their speeches at the opening ceremony and will visit the nearby villages of both the sides of the crossing points
    A lot of reports are already online and thanks to live-streaming, you no longer need a golden ticket to hear the discussions. hollister clothing store That’s the way it’s been the whole year, Capers said. That’s one of the most gratifying things about this team, because nobody has used it as an excuse. When guys have gone down, we come in and say, ‘Who’s got it to win and who’s going to go in there and play?’ x michael kors outlet online
    They may be tasty, but mixed drinks made with sugary syrups are laden with empty calories. Skip the eggnog and the madori sour, and instead choose from a wide array of wines and light beers. You ll get your holiday buzz without breaking the calorie bank! (510) 251-0329

  • June 7, 2015 at 5:26 am
  • Mobile Casino angeboten June 22, 2015 at 10:58 pm

    Vielen Dank! Wollt ich nur mal sagen.

Post comment