Answered by:
Dependency Injection?

Question
-
User2118885047 posted
Hi,
I have read some articles over DI, but not clear still. Can any one explain me in SHORT. WITH EXAMPLE PLEASE
Friday, September 16, 2011 11:56 AM
Answers
-
User-989998403 posted
DI is primarily about reducing the dependencies of your code and writing more testable code. For example, you reference having your DB code in a static class. Well the class using that static class is now tightly coupled to it. You are unable to write unit tests without hitting the database, generally unit tests should have no external dependencies.
If you abstract the data access layer out and inject an interface, you can switch out concrete implementations with fake or mock impelementations. Thus allowing your code to be easily testable.
Hope that helps a little.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 16, 2011 2:35 PM
All replies
-
User-821857111 posted
See if this article is clearer than the others you have read: http://www.mikesdotnetting.com/Article/117/Dependency-Injection-and-Inversion-of-Control-with-ASP.NET-MVC
Friday, September 16, 2011 12:28 PM -
User2118885047 posted
Thanks Mike,
I understand what is DI, but
what is difference between calling a static method (i think its loosely coupled, all our Db coding will be in this method).
AND
DI?
More or less, i see both are same right?
Friday, September 16, 2011 12:45 PM -
User-989998403 posted
DI is primarily about reducing the dependencies of your code and writing more testable code. For example, you reference having your DB code in a static class. Well the class using that static class is now tightly coupled to it. You are unable to write unit tests without hitting the database, generally unit tests should have no external dependencies.
If you abstract the data access layer out and inject an interface, you can switch out concrete implementations with fake or mock impelementations. Thus allowing your code to be easily testable.
Hope that helps a little.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 16, 2011 2:35 PM -
User2118885047 posted
Yes exactly I as thinking of this.
As I am new to MVC, I need to do some practice tests, to make this understand perfectly. Thanks.
Friday, September 16, 2011 2:43 PM