IOC implements multiple interfaces

In the templates I typically see code such as

services.AddSingleton<IAuthRepository>(c =>
                new InMemoryAuthRepository<AppUser, UserAuthDetails>());

How can I register the singleton as implementing both IAuthRepository and IUserAuthRepository?

Because I’m using OrmLiteAuthRepository, which implements both, and I’d like to go and Resolve at some point. Plan B is to just Resolve() as IUserAuthRepository …

You don’t need to with IAuthRepository as all the IUserAuthRepository APIs are available as extension methods on IAuthRepository so you can just call it from the IAuthRepository interface directly.

FYI inside a Service you would access it from base.AuthRepository or base.AuthRepositoryAsync, outside of a Service you can resolve it from HostContext.AppHost.GetAuthRepository().

For other dependencies you would still register it once and cast it when you need the extra functionality.

I need it to call RecordInvalidLoginAttempt in my custom auth provider, which extends CredentialsAuthProvider.

From inside TryAuthenticateAsync
var userRepo = authService.Resolve<IAuthRepository>() as IUserAuthRepositoryAsync;
works fine though, I just wanted to learn about the IOC’s possibilities.

1 Like