2. Dependency Injector Configuration - JamesRandall/AzureFromTheTrenches.Commanding GitHub Wiki

The framework is designed for use with an IoC container but is agnostic of any specific container - which makes it flexible but does make the wiring a little more tricky. If you are using the Microsoft.Extensions.DependencyInjection framework for I have a handy helper or you can use any other framework with just a little more typing!

Using Microsoft.Extensions.DependencyInjection

Add the helpers NuGet package as follows:

Install-Package AzureFromTheTrenches.Commanding.MicrosoftDependencyInjection

And then if you're creating the IServicesCollection yourself use code such as the following:

IServiceCollection serviceCollection = new ServiceCollection();
// register your dependencies e.g.
// serviceCollection.AddTransient<MyInterface,MyImpl>();
IMicrosoftDependencyInjectionCommandingResolver helper = new MicrosoftDependencyInjectionCommandingResolver(services);
ICommandRegistry registry = helper.UseCommanding();
// register your handlers e.g.
// registry.Register<MyCommandHandler>();
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
helper.ServiceProvider = serviceProvider;

Because the Microsoft injector requires a build step before dependencies can be resolved it's vitally important that the final line (that assigns the service provider to the helper) is in place and occurs after all your registration. This enables the commanding framework to use your IoC configuration to construct command handlers.

If you're using ASP.Net Core than your Startup class should be adjusted to look something like this:

public class Startup
{
    private IMicrosoftDependencyInjectionCommandingResolver _commandingDependencyResolver;

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        _commandingDependencyResolver = new MicrosoftDependencyInjectionCommandingResolver(services);
        ICommandRegistry registry = _commandingDependencyResolver.UseCommanding();
        // Register your handlers e.g.
        // registry.Register<MyCommandHandler>();            
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        _commandingDependencyResolver.ServiceProvider = app.ApplicationServices;
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseMvc();
    }
}

Again it's vitally important that the ServiceProvider property is assigned to the helper as can be seen in the first line of the Configure() method.

Using Other Containers

If you're using another container then a shim is provided that should be constructed using functions:

IContainer myContainer = new Container();
// register your dependencies e.g.
// myContainer.Register<IDoSomething,DoSomething>();
ICommandingDependencyResolver shim = new CommandingDependencyResolver(
    (type,instance) => myContainer.RegisterInstance(type, instance),
    (interfaceType, implementationType) => myContainer.RegisterTransient(interfaceType, implementationType),
    (type) => myContainer.Resolve(type)
);
ICommandRegistry registry = shim.UseCommanding();
// register your handlers e.g.
// registry.Register<MyCommandHandler>();

If your container requires a build step then it should be built before any commands are executed.