How to register many for open generic in Autofac

后端 未结 2 1999
挽巷
挽巷 2020-12-28 14:55

I\'m new to Autofac (not to DI). Here is the situation:

I have these interfaces:

public interface IQuery

        
相关标签:
2条回答
  • 2020-12-28 15:36

    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<>);
    
    0 讨论(0)
  • 2020-12-28 15:47

    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();
    
    0 讨论(0)
提交回复
热议问题