/AgentMulder

Agent Mulder plugin for ReSharper provides navigation to and finding usages of types registered or resolved via Dependency Injection (DI) containers.

Primary LanguageC#BSD 2-Clause "Simplified" LicenseBSD-2-Clause

Agent Mulder

Agent Mulder plugin for ReSharper analyzes DI containers (Dependency Injection, sometimes called Inversion of Control, or IoC containers) in your solution, and provides navigation to and finding usages of types registered or resolved via DI containers.

Download Agent Mulder v1.0.0 Beta here!

###Check out a short demonstration video here: http://vimeo.com/41113265 ### (GitHub does not allow embedding videos in the README page)

The Problem

With all the major benefits of using smart DI containers to wire up dependencies in code, it breaks automatic code-analysis tools like ReSharper, because DI containers will only create concrete types at run-time (typically using Reflection), and this information is not known during static code analysis.

Consider the following example with Castle Windsor:

var container = new WindsorContainer();
container.Register(
    Component.For<IMessageWriter>().ImplementedBy<ConsoleMessageWriter>()
    );
    
var messageWriter = container.Resolve<IMessageWriter>();

Using the configuration above, at runtime the container will resolve the concrete ConsoleMessageWriter, however, if you're using ReSharper with solution-wide analysis enabled, it will tell you that ConsoleMessageWriter is never instantiated:

Never instantiated

This gets worse in convention-based registrations, such as:

container.Register(
    Classes.FromThisAssembly().BasedOn<IMessageWriter>()
    );

Where ReSharper will not even know that this type is being used:

Never used

Agent Mulder aims to solve that problem!

The Solution

Agent Mulder plugin for ReSharper analyzes known DI container registrations in the entire solution, and adds the missing information to ReSharper, so it no longer flags concrete types as being unused. It even adds an visual icon (Black Magic Hat) to the type name, allowing you to navigate to the exact line, where the concrete type is being implicitly or explicitly registered:

Agent Mulder

Agent Mulder plugin also adds a new navigation feature, allowing you to navigate to all the types that are being registered via any particular registration. Simply position the caret on the registration expression, and press the Navigate To keyboard shortcut (Ctrl-Shift-G in IntelliJ IDEA bindings, Alt-` in Visual Studio bindings):

Navigate To

Select Registered Components, and you will be presented with a list of types matching that registration:

Results

Pressing + on the numeric keypad (or clicking the little search icon in the caption) will open the results in a tool window:

Tool window

Building and installing Agent Mulder plugin

To build the plugin you need to have the ReSharper SDK (v6.1.x at the time of writing), installed on your machine. The project will add the references automatically.

To build the plugin, run the file src\build.cmd. The files will be built and placed into the directories output\Debug and output\Release. In the directory will also be a batch file that copies the files into the ReSharper's plugin directory. You will need to restart Visual Studio to see the changes.

Frequently Asked Questions (last updated: May 18th, 2012)##

Q: Wow! How does it work?

A: Agent Mulder makes heavy use ReSharper's Structural Search to look for registration patterns, such as Component.For<$service$>(). This makes searching very efficient and does not require any additional parsing. This also allows for adding new patterns easily, allowing adding support for additional DI containers.

More information is available in the wiki page.

Q: What DI containers are currently supported?

A: The list of the currently supported DI containers and their syntax can be found on the wiki page.

Q: What about X (Ninject/StructureMap/Unity)? Can you add support for it?

A: Great question! Suggest a feature on the issue tracker, or better yet, send me a pull request!

Q: Why the name Agent Mulder?

A: There already exist a few great plugins for ReSharper called Agent Smith, Agent Johnson and Agent Ralph. I decided to continue the tradition, and after some consideration I decided on Agent Mulder - The IoC Investigator :)

Q: I found a bug/Agent Mulder highlights the wrong type (or doesn't)/It doesn't work!

A: Great! Let me know about it, and I will try to fix it! Please note that I don't know all DI containers and their rules - if you think a mistake in analysis has been made, please note what should be the desired outcome in the issue.

Q: What font did you use for the logo?

A: It is called simply "The X-Files" by SpideRaY, and is available to download here.

Also please note that Agent Mulder plugin requires ReSharper 6.1 and only works in Visual Studio 2010. Other versions were not tested!

Happy Investigating!