12

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?

Steven
  • 166,672
  • 24
  • 332
  • 435
Thomas
  • 469
  • 6
  • 16
  • Possible duplicate of http://stackoverflow.com/questions/620376/how-do-you-find-all-implementations-of-an-interface – undefined Jan 09 '14 at 21:39
  • 2
    Buy ReSharper and become friends with Alt + End. – Simon Whitehead Jan 09 '14 at 21:42
  • This question is not already asked. I know you can use resharper but it doesn't help when you use DI. Resharper doesn't know which implementation class was configured, it will just list all classes implementing that interface. I want it to take me to the configured class. I also asked about how you people tackle this. – Thomas Jan 09 '14 at 21:51
  • 1
    @Thomas, how many alternative implementations do you have? In my experience it is usually either an actual production implementation or a testing mock that I want, so there is never any doubt. – 500 - Internal Server Error Jan 09 '14 at 23:29
  • 1
    It's as you say almost two implementations. But it doesn't matter, you still need to find the file with the implementation and open it manually. This takes time and you lost focus. I know resharper gives you a list where you can choose implementation. Are there other alternatives (free)? – Thomas Jan 10 '14 at 12:36

1 Answers1

13

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Thanks, will check it out. I'm mainly interested in this when going through code someone else has written. For example when taking over a application from a project into maintenance. It's important to be able to navigate the code without debugging quite often. From my experience it's very common that the applications are not well designed :( – Thomas Jan 10 '14 at 16:42
  • @Thomas: I second that. But at least you seem to be working on a code base that actually does DI. That's something :-) – Steven Jan 10 '14 at 17:33