In what scenarios is dependency injection helpful?
-
I hear a lot about dependency injection and and DI frameworks these days. I don't work at a place that uses it. I'm having trouble envisioning it's use. When does dependency injection make your code clearer? Is it mostly used for testing?
-
Answer:
I think Dependency Injection is one of those things that was done for one reason initially (decoupling implementations of dependencies from their use) but turned out to be really meaningful in another way (code clarity). This is because it turns out that creating and marshalling collaborators is a cross cutting concern that can be abstracted from the business logic that requires the collaborators. so: public void validateAndSave(Thing thing) { Validator validator = new WebThingValidator(); if (validator.validate(thing)) { DatabaseConnectionManager connectionManager = DatabaseConnectionManagerFactory.instance(); ThingDataAccessObject dao = new ThingDataAccessObject(connectionManager); dao.save(thing); } } where collaborators have to be retrieved or instantiated by the same code that is using them compared with: public void validateAndSave(Thing thing) { if (validator.validate(thing)) { dao.save(thing); } } where dependency injection takes care of supplying the collaborators. You can see that the logic is as complete in the second example, but FAR more readable. Bear in mind that code hasn't exactly been eliminated, simply moved, usually to a config file such as xml (I'm not quite sure whether using annotations counts as eliminating code or not). But the separation of concerns leads to much easier to read business logic. You also have decoupled collaboration. This means that writing pure unit tests is much easier in that your resulting business logic classes can have their collaborators set on them, so it's easy to plug in stubs and mocks for testing. One thing to remember is that, even though you've moved your collaborator wiring out of business logic classes, you still have to test it. The wiring logic can go wrong just as much as other code (and, in my experience, usually does more often). Write integration tests that make sure your wiring is correct.
James Bromley at Quora Visit the source
Other answers
Now i am a java guy always worried about Heap. As object grew in heap we all know performance go down for GC. Dependency injection is a design technique which came into picture for many reason. But the most important reason is reuse. In a large project with 1000 java programmer the project (on the lower level) ends up in code which create duplicate objects. with Independence injection Single object can be used at many different place (by keeping this single object alive in memory). To support my answer if know spring. You will know the benifit of DI. In a large project places were it is highly used is 1. single user authentication object. 2. single service object. 3. single db access object. (now don't think in a project there are only single service and single db objects. these object are specific to one flow of task). Eample: 1. EmployeeService. 2. EmployeeDao. all of them connected ( i am mean to say injected with other were required). Hope that helps.
Ahmed Jamal Maaz
Dependency Injection's main usecase is to remove tight coupling code which inturn helps in testing objects independently by creating mock objects
Sudan Shrinivaasan
Related Q & A:
- What degree is the most helpful in computer programming?
- What are some helpful websites for teens?Best solution by Yahoo! Answers
- What are some easy and helpful studying tips for highschool?Best solution by Yahoo! Answers
- Dependency on the FAFSA?Best solution by Yahoo! Answers
- What is dependency service?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.