An ADR Visual Studio Extension – Dependency Injection

pcmichaels profile image Paul Michaels Originally published at pmichaels.net ・2 min read

Continuing with my little series on creating a visual studio extension, in this post, I'll talk about how to add dependency injection to your project.

If you'd like to see the whole solution for this, it can be found here.


In this post on Azure Functions, I talked about using Unity as an IoC container, in a place where an IoC container might not necessarily fit; whilst this is no longer true for Azure functions, it does appear to be for extensions - I presume because they don't expect you to have one big enough to warrant IoC; also, even with DI, testing is very difficult, because most of what you're doing, you're doing to Visual Studio.

Let's start by installing Unity in our project:

Install-Package Unity

Rules Analyser

In our case, we were analysing the project and extracting files; however, we were extracting all files; as a result, a check needed to be made to extract only markdown files. Consequently, I created a RulesAnalyser class:

    public class RulesAnalyser : IRulesAnalyser
        public bool IsProjectItemNameValid(string projectItemName) =>

We could (and I did initially) instantiate that directly in the ViewModel, but that feels quite dirty.


The *Package file for the extension seems to be the entry point, so we can add the unity container to here:

    public sealed class AdrPackage : AsyncPackage
        public static Lazy<IUnityContainer> UnityContainer =
         new Lazy<IUnityContainer>(() =>
             IUnityContainer container = InitialiseUnityContainer();
             return container;

        private static IUnityContainer InitialiseUnityContainer()
            UnityContainer container = new UnityContainer();
            container.RegisterType<IRulesAnalyser, RulesAnalyser>();
            container.RegisterType<ISolutionAnalyser, SolutionAnalyser>();
            return container;

        . . .

View Model

The next thing we need to do is to inject our dependencies.

        public AdrControlViewModel() 
            : this(AdrPackage.UnityContainer.Value.Resolve<IRulesAnalyser>(),

        public AdrControlViewModel(IRulesAnalyser rulesAnalyser, ISolutionAnalyser solutionAnalyser)
            _rulesAnalyser = rulesAnalyser;
            _solutionAnalyser = solutionAnalyser;

            Scan = new RelayCommandAsync<object>(ScanCommand);

And that's it, we now have a working DI model in our project.




Posted on by:

pcmichaels profile

Paul Michaels


I'm a Lead Developer. I've been programming professionally since 1997. I'm interested in finding neat solutions to difficult problems.


Editor guide