natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] UnityContainer for IoC?

rcdailey opened this issue · comments

I would like to use UnityContainer as my IoC container in my application. I'm still new to CLU, but I'd like for the command classes to be "dumb data" if possible. In other words, bundling logic in the OnExecute() methods should be avoided. Actually, I think the only reason I feel this way is because I can't easily tie it into my composition root where I use UnityContainer. Your documentation shows ways to register interfaces for injecting dependencies, but I'd rather not use that.

Only solution I can think of is to construct my UnityContainer in my BaseCommand, and have it available downstream for overridden OnExecute() methods.

For reference, I'm using the inheritance example for subcommand processing. Thanks for any architectural advice you can give.

I found a solution to this. Basically I just need to implement IServiceProvider like so:

internal class UnityServiceProvider : IServiceProvider
{
    private readonly IUnityContainer _container;

    public UnityServiceProvider(IUnityContainer container)
    {
        _container = container;
    }

    public object GetService(Type serviceType)
    {
        return _container.Resolve(serviceType);
    }
}

And pass this into app.Conventions.UseConstructorInjection().