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 support #33

Open
brgrz opened this issue Aug 9, 2014 · 2 comments
Open

AutoFac support #33

brgrz opened this issue Aug 9, 2014 · 2 comments

Comments

@brgrz
Copy link

brgrz commented Aug 9, 2014

Does it work with Autofac (or any other DI container)? Can it be plugged in?

@lruckman
Copy link

There is an ShortBus Autofac Marker NuGet package but I do not know if it'll do what you need.

@jrnail23
Copy link
Contributor

@brgrz, there is an Autofac integration package -- see AutofacBasicExample, particularly the container building code (adapted from the AutofacBasicExample.SetUpRootScope method):

var builder = new ContainerBuilder();

// this is needed to allow the Mediator to resolve contravariant handlers (not enabled by default in Autofac)
builder.RegisterSource(new ContravariantRegistrationSource());

// you'll need to make sure to include any assemblies that contain your handlers here
var assembliesToScan = new []{typeof (IMediator).Assembly, GetType().Assembly}

builder.RegisterAssemblyTypes(assembliesToScan)
    .AsClosedTypesOf(typeof (IRequestHandler<,>))
    .AsImplementedInterfaces();

builder.RegisterType<Mediator>().AsImplementedInterfaces().InstancePerLifetimeScope();

// to allow ShortBus to resolve lifetime-scoped dependencies properly, 
// we really can't use the default approach of setting the static (global) dependency resolver, 
// since that resolves instances from the root scope passed into it, rather than 
// the current lifetime scope at the time of resolution.  
// Resolving from the root scope can cause resource leaks, or in the case of components with a 
// specific scope affinity (AutofacWebRequest, for example) it would fail outright, 
// since that scope doesn't exist at the root level.
builder.RegisterType<AutofacDependencyResolver>()
    .AsImplementedInterfaces()
    .InstancePerLifetimeScope();

var container = builder.Build();

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

3 participants