Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging dependency injection in .NET

When using DI it's harder to read the code since you have alot of interfaces everywhere. i.e., you can't just hit F12 (go to definition) in Visual Studio since that only takes you to the interface. You need to know which class is configured to be used.

Is there a plugin or something like that which makes this easier? How are you people tackling this?

like image 987
Thomas Avatar asked Jan 09 '14 21:01

Thomas


1 Answers

In Visual Studio 2015 and up, you can hit CTRL + F12 and that will jump directly to the implementation if there is only one, and otherwise will prompt a list of implementations to choose from. This makes it easy to navigate your code from inside your IDE.

There's a plugin for ReSharper called Agent Mulder that integrates ReSharper with Dependency Injection libraries. It allows you to see which classes are in use and allows you to jump directly to the interface's configuration or its implementation.

But to be honest, debugging the code with DI doesn't change, since you can still step into methods calls while debugging, as you're used to.

I found that in a well-designed application, I find myself less jumping from class to class while navigating through the code while working on a new feature. This typically happens because the new classes I write for that feature work reasonably without requiring knowledge about its dependencies. It's not to say that DI immediately leads to well-designed code, but it's just another tool in your toolbox that can help in making code more maintainable.

But even if browsing code and debugging would become (a bit) harder, being able to plug in new features, add coss-cutting concerns, and being able to test an application will have a enormous positive impact on the overall quality and maintainability of an application.

like image 126
Steven Avatar answered Sep 26 '22 13:09

Steven