Should I inject an interface or its implementation ?
I came across the below code in a tutorial and I am wondering why inject the interface but register it as its implementation when you can just directly inject its implementation?
SIGNINBLOC
@injectable
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final IAuthFacade _authFacade;
SignInBloc(this._authFacade) : super(SignInState.initial());
FIREBASEAUTH
@LazySingleton(as: IAuthFacade)
class FirebaseAuthFacade implements IAuthFacade {
INJECTION CONFIG
gh.lazySingleton<IAuthFacade>(
() => FirebaseAuthFacade(get<FirebaseAuth>(), get<GoogleSignIn>()));
You want to separate the implementation details and dependency to a specific implementation. It will be easier to switch between different implementations and also easier to mock the interface when testing.
You can for example configure GetIt to use different implementations for the interface depending on if it is e.g. development environment, production environment, test environment etc.. You only have to change one line and it will change everywhere.