How to register many for open generic in Autofac
If you have a single concrete generic type, and don't want the scanning overhead and improve the startup performance, you can register as below:
builder.RegisterGeneric(typeof(ConcreteGenericType<>)).As(typeof(IServiceType<>);
You can do this with Autofac you just need to use the scanning feature and use the AsClosedTypesOf
method:
AsClosedTypesOf(open)
- register types that are assignable to a closed instance of the open generic type.
So your registration will look like this:
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AsClosedTypesOf(typeof (IQueryHandler<,>)).AsImplementedInterfaces();