How to register many for open generic in Autofac

后端 未结 2 2006
挽巷
挽巷 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: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();
    

提交回复
热议问题