Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection and code maintainability

I am working on a (vb.net/asp.net) project that is using interfaces to provide dependency injection. But to me, it feels like the maintainability of the code has been killed. When I want to read through the code, I can't simply jump to the code of a related class that is used. All I see are the interfaces, and so I have to hunt through the project to figure out what classes are doing the implementation. This really hurts my productivity.

Yes, I know I now can implement the interfaces with a wide variety of replacement classes. But for example, I know I'm not changing my data source any time soon--there is no need for me to enable the ability to swap that out. All of this dependency injection seems like overkill to me (in fact, the only real reason it is there is to support mock classes for unit testing). I've actually read several places that state DI is actually better for maintainability. But that assumes you already know where everything is and you know which class you need to update. Finding out where to look is the part that is killing me.

So, my question is: Is there a better way to traverse through the code? Is there a better way to make the code more maintainable? Are we just doing it wrong? Or is this par for the course?

like image 999
JLX Avatar asked Oct 30 '10 05:10

JLX


People also ask

What is the main purpose of using dependency injection?

Dependency injection helps to develop testable code, allowing developers to write unit tests easily. You can use mock databases with dependency injection, and test your application without affecting the actual database. Here's an example.

What are the three types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).

What is dependency injection and what is it main advantage disadvantage?

Dependency injection creates clients that demand configuration details to be supplied by construction code. This can be difficult when obvious defaults are available. Dependency injection can make code difficult to trace (read) because it separates behaviour from construction.

What problem does dependency injection solve?

Dependency injection is a technique used in software engineering to develop loosely coupled systems. It is a technique that allows an object to receive other objects that it depends on by some other program than it explicitly calling the dependent object.


2 Answers

There is definitely some overhead to DI, especially when your configuration is separated from your code. While this is par for the course, it does get easier to deal with over time, and as you get a better understanding of the code.

However, there is tooling that can help - Have a look at Resharper or CodeRush. Both offer excellent improvements to the code navigation experience in Visual Studio. Resharper has excellent "Go To Symbol" or "Go To Implementation" methods that quickly help you navigate to the implementation of your interface, wherever it may be.

To the point about maintainability: In general, a loosely coupled design becomes more important as time passes, because there will be change. The more tightly coupled your code is, the harder it is to make small changes without affecting the overall application. This is where depending on interfaces is very very important -- whether or not you choose to use Dependency Injection.

like image 114
Nader Shirazie Avatar answered Sep 30 '22 15:09

Nader Shirazie


Maintainability is many different things. Overall, it addresses the degree to which you can keep evolving an application by adding new features.

Yes, it may become more difficult to understand how collaborators are connected, so that aspect of maintainability may suffer by introducing loose coupling.

However, once you've figured out how the code base works, you should be better able to add new features without slowing down. In that sense, maintainability is much improved by loose coupling.

It's not a silver bullet, though. Loose coupling is a prerequisite for maintainable code, not a guarantee.

like image 41
Mark Seemann Avatar answered Sep 30 '22 15:09

Mark Seemann