Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofac integration #7

Closed
msepahvand opened this issue Nov 5, 2016 · 2 comments
Closed

Autofac integration #7

msepahvand opened this issue Nov 5, 2016 · 2 comments

Comments

@msepahvand
Copy link

Hi @gautema,

Thanks for the wonderful project. I have more of a question than an issue...I'm trying to get the sample project to work with Autofac, I've gone about doing that by converting the .net core DI code to Autofac, here is my startup.cs:

public class Startup
{
    public IContainer ApplicationContainer { get; private set; }
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
        var builder = new ContainerBuilder();
        services.AddMvc();
        // services.AddSingleton<InProcessBus>(new InProcessBus());
        builder.Register<InProcessBus>(x => new InProcessBus()).AsSelf().SingleInstance();
        builder.Register<ICommandSender>(c => c.Resolve<InProcessBus>()).SingleInstance();
        builder.Register<IEventPublisher>(c => c.Resolve<InProcessBus>()).SingleInstance();
        builder.RegisterType<Session>().As<ISession>().InstancePerLifetimeScope();
        builder.RegisterType<InMemoryEventStore>().As<IEventStore>().SingleInstance();
        builder.RegisterType<MemoryCache>().As<ICache>().InstancePerLifetimeScope();
        builder.Register<IRepository>(x =>
            new CacheRepository(new Repository(x.Resolve<IEventStore>()),
            x.Resolve<IEventStore>(), x.Resolve<ICache>())).InstancePerLifetimeScope();
        builder.RegisterType<ReadModelFacade>().As<IReadModelFacade>().InstancePerDependency();
        var targetAssembly = typeof(InventoryCommandHandlers).GetTypeInfo().Assembly;

        builder.RegisterAssemblyTypes(targetAssembly)
       .Where(type => type.GetInterfaces().Any(y => y.IsAssignableFrom(typeof(CQRSlite.Commands.ICommandHandler<>))))
       .AsImplementedInterfaces()
       .InstancePerDependency();
        

        builder.Populate(services);
        this.ApplicationContainer = builder.Build();

        var sp= new AutofacServiceProvider(ApplicationContainer);
        var registrar = new BusRegistrar(new DependencyResolver(sp));
        registrar.Register(typeof(InventoryCommandHandlers));

        return sp;
    }

}

However when i step through this, the GetService Method in DependecyResolver.cs can not resolve IHandlerRegistrar.

Any guidance would be appreciated.

@msepahvand
Copy link
Author

msepahvand commented Nov 6, 2016

Duh...I forgot to register IHandlerRegistrar, it was late at night :)

Here is the autofac equivalent:

msepahvand@7b9802d

@gautema
Copy link
Owner

gautema commented Nov 6, 2016

Great 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants