I have some classes like UserQuery, CustomerQuery implementing interfaces like IUserQuery, ICustomerQuery respectively. In my binding configuration I need to bind every interface with the respectively query:
builder.RegisterType<UserQuery>().As<IUserQuery>().InstancePerRequest();
builder.RegisterType<CustomerQuery>().As<ICustomerQuery>().InstancePerRequest();
This is working pretty fine, but I was wondering if there is a way to make a convention-based binding in place of binding every single classe XXX[Query] -> [I]XXX[Query].
I'm using AutoFac as my DI container.
I'm not a AutoFac experienced user. However after some research a tested the code below with success:
var assembly = Assembly.GetExecutingAssembly();
builder
.RegisterAssemblyTypes(assembly)
.AssignableTo<IQuery>()
.AsImplementedInterfaces()
.InstancePerRequest();
The IQuery interface above is just a tag interface that should be inherited from every query interface that you have. Using your example:
Interfaces
IUserQuery: IQuery
ICustomerQuery: IQuery
Classes
UserQuery: IUserQuery
CustomerQuery: CustomerQuery
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With