daniellittledev/Enexure.MicroBus

New IoC container - IServiceProvider in AspNetCore

lessismore1 opened this issue · 8 comments

Hi Daniel,

I have looked at mediatr and this and found your implementation "attractive".
I would like to have an implementation for AspNetCore IoC.

Made an attempt but run into trubble :)

Best,

using Enexure.MicroBus;
using Microsoft.Extensions.DependencyInjection;

namespace Estate.Write
{
public static class ServiceCollectionExtension
{
public static IServiceCollection RegisterMicroBus(this IServiceCollection services, BusBuilder busBuilder)
{
return RegisterMicroBus(services, busBuilder, new BusSettings());
}

    public static IServiceCollection RegisterMicroBus(this IServiceCollection services, BusBuilder busBuilder, BusSettings busSettings)
    {
        services.AddSingleton(busSettings);

        var pipelineBuilder = new PipelineBuilder(busBuilder);
        pipelineBuilder.Validate();

        RegisterHandlersWithDotNetCore(services, busBuilder);

        services.AddSingleton<IPipelineBuilder>(pipelineBuilder);
        services.AddScoped<IOuterPipelineDetector, OuterPipelineDetector>();
        services.AddScoped<IPipelineRunBuilder, PipelineRunBuilder>();
        services.AddScoped<DotNetCoreDependencyScope>();

        services.AddScoped<IMicroBus, MicroBus>();
        services.AddScoped<IMicroMediator, MicroMediator>();

        return services;
    }

    private static void RegisterHandlersWithDotNetCore(this IServiceCollection services, BusBuilder busBuilder)
    {
        foreach (var globalHandlerRegistration in busBuilder.GlobalHandlerRegistrations)
        {
            services.AddTransient(globalHandlerRegistration.HandlerType);

            foreach (var dependency in globalHandlerRegistration.Dependencies)
            {
                services.AddTransient(dependency);
            }
        }

        foreach (var registration in busBuilder.MessageHandlerRegistrations)
        {
            services.AddTransient(registration.HandlerType);

            foreach (var dependency in registration.Dependencies)
            {
                services.AddTransient(dependency);
            }
        }
    }
}

}

using System;
using System.Collections.Generic;
using Enexure.MicroBus;
using Microsoft.Extensions.DependencyInjection;

namespace Estate.Write
{
public class DotNetCoreDependencyScope : DotNetCoreDependencyResolver, IDependencyScope
{
private readonly IServiceProvider _provider;

    public DotNetCoreDependencyScope(IServiceProvider provider)
        :base(provider)
    {
        this._provider = provider;
    }

    public object GetService(Type serviceType)
    {
        return _provider.GetRequiredService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return (IEnumerable<object>)_provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
    }

    public T GetService<T>()
    {
        return (T)GetService(typeof(T));
    }

    public IEnumerable<T> GetServices<T>()
    {
        return (IEnumerable<T>)GetService(typeof(IEnumerable<T>));
    }

    public void Dispose()
    {
    }
}

}

    public void ConfigureServices(IServiceCollection services)
    {
        // Add application services.
        services.AddScoped<Context>();

        var busBuilder = new BusBuilder()
            
            // Global handlers run in order so these are explicitly registered
            //.RegisterGlobalHandler<LoggingHandler>()
            //.RegisterGlobalHandler<SecurityHandler>()
            //.RegisterGlobalHandler<ValidationHandler>()
            .RegisterGlobalHandler<TransactionHandler>()

            // Manually add
            //.RegisterCommandHandler<LoginCommand, LoginCommandHandler>();

            // Scan an assembly to find all the CommandHandlers and Commands
            .RegisterHandlers(typeof(Context).GetTypeInfo().Assembly);

        services.RegisterMicroBus(busBuilder);

    }

Hi There!

Thank you for checking out MicroBus, I've love to know what exactly you found attractive about it!

You're right, I think having Microsoft.DependencyInjection support would be a great addition. I think I might take a stab at it myself (looks like I'll have some time today).

What issues did you run into with your attempt?

I've created a package for MicroBus Microsoft DI support

Install-Package Enexure.MicroBus.MicrosoftDependencyInjection

Let me know how you go with it!

Thx!

I will have a try tomorrow afternoon. Will inform you about the progress :)

I intended to use mediatr in a project, when I came across two of your articles:
comparing-microbus-and-mediatr
building-a-microbus-application

From the first article, I found that the "WireUp" seemed to be simpler and the "Api" was more inline with my expectations

The second article described your used concepts very good.

All in all - you code in a way I like!

Couldn't wait - had to make a short test..
I works like a charm - thx again!

Best Regards
Ronny

Bus, CrossCutting, etc seems to be working!