Agent Mulder – supporting IoC in ReSharper

The almost pervasive use of IoC containers in modern-day enterprise apps presents a special challenge to ReSharper. After all, how can ReSharper possibly know that a type, which for all intents and purposes appears to be unused, is actually configured to be instantiated implicitly by using a dependency injection mechanism?

Some might say that this is impossible, since the wide variety of IoC containers coupled with various distinct initialization options make the detection of component initialization a real challenge. This challenge was met, however, by Igal Tabachnik (@hmemcpy), the creator of the Agent Mulder plugin.

At the time of writing, Agent Mulder supports the following containers:

  • Castle Windsor

  • NInject

  • Unity

  • Autofac

  • StructureMap

  • Catel IoC

Depending on the container in question, Agent Mulder supports two types of component registration:

  • Manual registration involves registering a single component type in the container. This is typically done by providing the component’s type, so for example with Unity one might invoke RegisterType<Foo>() or RegisterType(typeof(Foo),...) to add the component to the container.

  • Convention-based registration allows bulk registration of components in the container via a particular method. For example, Autofac’s InNamespaceOf<T>() method allows the user to register all types that can be found inside the namespace inhabited by type T.

While it is technically infeasible for Agent Mulder to support every variety of configuration in every container, it does manage to cover a very large percentage of configuration options in the aforementioned containers, as documented in its Wiki pages. But, apart from analyzing container registrations, what does the plugin actually do?

Ultimately, Agent Mulder does three things:

  • The main goal of Agent Mulder is to suppress unused ispections when a container-registered component does not appear to be instantiated.

  • In addition, Mulder also uses a gutter mark to indicate a container-registered component. One can click this gutter mark to navigate to the exact line where this component is registered:

  • Finally, one can also navigate to registered component right from the statement that performs the registration:

Mulder is an open-source plugin hosted on GitHub. It appears to have an active user base, so if you are interested in, say, a container it doesn’t support, feel free to open a request or implement it yourself and send a pull request. Meanwhile, have fun with IoC and stay tuned for more plugin reviews!

This entry was posted in ReSharper Tips&Tricks and tagged , , , , , , , , , , . Bookmark the permalink.

6 Responses to Agent Mulder – supporting IoC in ReSharper

  1. MEF says:

    MEF would be great :)

  2. Adam says:

    Am I missing something or does it currently require users to download the sources and compile them (which in turn requires installing resharper’s sdk…)?

  3. Dmitri Nesteruk says:

    @MEF: support for many different frameworks would indeed be great. However, Igal can only cover so much. That said, feel free to help him!

    @Adam: you can find a link to the latest MSI installer on the following page: http://hmemcpy.github.com/AgentMulder/.

  4. Igal Tabachnik says:

    Thank you very much for the post! :)

    @MEF
    At this point, the Agent Mulder plugin utilizes ReSharper’s “Search and Replace” (SSR) mechanism to look for registration patterns in the code. Unfortunately, it has some limitations, such as looking for particular attribute types, therefore currently MEF is not supported. I am thinking about ways to solving it, so at the very least I can say that MEF support is coming, but I can’t say when.
    If you want to know more about how Agent Mulder works, I have some information on the wiki: https://github.com/hmemcpy/AgentMulder/wiki

    @Adam
    Ever since Github decided to remove the Downloads feature, the link is, unfortunately, not very visible. You can download it from the site here: http://hmemcpy.github.com/AgentMulder/

  5. Peter Mounce says:

    Is it possible to apply plugins to the new command-line inspection-runner that ships in R# 8, and has been available within TeamCity for a while?

  6. Dmitry Matveev says:

    @Peter
    Currently, it is impossible. However, we plan to implement that soon, in one of the coming releases of ReSharper Command Line Tools. Please stay in touch.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">