Currently i have a webapi controller with the constructor like this:
readonly IQueryFactory queryFactory;
readonly ICommandFactory commandFactory;
public UserBenefitsController(
IQueryFactory queryFactory,
ICommandFactory commandFactory)
{
this.queryFactory = queryFactory;
this.commandFactory = commandFactory;
}
I am using simple injector to register both of these types.
container.RegisterWebApiRequest<IQueryFactory, QueryFactory>();
container.RegisterWebApiRequest<ICommandFactory, CommandFactory>();
But i am finding that as i continue to develop my app that I am continuing to have a lot of ISomeInterface
resolving to ISomeConcrete
with 1:1 ratio.
Is it possible to tell simple injector to look for at an interface and automatically resolve it when there is only 1 concrete for it within the WebApiRequest scope?
You can use batch / automatic registration to resolve to concrete instances. The Simple Injector documentation explains it here
For instance:
ScopedLifestyle scopedLifestyle = new WebApiRequestLifestyle();
var assembly = typeof(YourRepository).Assembly;
var registrations =
from type in assembly.GetExportedTypes()
where type.Namespace == "Acme.YourRepositories"
where type.GetInterfaces().Any()
select new
{
Service = type.GetInterfaces().Single(),
Implementation = type
};
foreach (var reg in registrations)
container.Register(reg.Service, reg.Implementation, scopedLifestyle);