1

I've multiple classes that inherit from a base class.

The base class should contain the Http instance so that subclasses, could issue HTTP calls in addition to their logic. The problem is that the subclasses need to call the super method from their constructor function, but I don't want to pass along the HTTP instance from bottom up.

Any ideas?

export abstract class AuthBaseService {

  constructor(protected http: Http) {

  }

  public abstract login(credentials);

  public abstract logout();
}



export class FacebookProviderAuthService extends AuthBaseService {

  constructor(private facebookAuth: Auth) {
     // **PROBLEM : I MUST CALL SUPER HERE** 
  }

  // Login & Logout impl.
}
0

2 Answers 2

1

I'm not sure what you mean by not wanting to pass it from bottom up. The AuthBaseService class is abstract, so an instance can't be created. All dependencies must be injected from the subclasses, because any DI framework can only have access to an instance of the subclass. This really isn't breaking the idea of dependency injection, and is supported by most DI frameworks I've seen. A FacebookProviderAuthService has a dependency on an Http instance, and so it expects it to be passed in.

One workaround of course is to just instanciate the Http instance in the super class constructor, but that's definitely breaking the dependency inversion. I guess I'm not exactly sure what the problem is. In my eyes, this doesn't seem like a problem at all.

Think of it like this. Suppose you want to unit test the FacebookProviderAuthService. If it doesn't take in an Http dependency as part of its constructor, how would you be able to mock it out? The FacebookProviderAuthService needs to be explicit about its dependencies, even if some of those dependencies come from the base abstract class.

Sign up to request clarification or add additional context in comments.

3 Comments

what if I want to compose httpclient only in base class but not in subclass instead have some protected methods which base call? the composition will work?
You can definitely have the protected methods that call the base class, but if you're creating a "subclass" type, you'll still need to pass in the HttpClient. By requiring the HttpClient in the Base Type, you're saying that all instances (including subclasses) of this type require an Http Client. If you have a base "Person" class with a required "Name" parameter, then you're saying that all "Persons" have a name. If you later make a "Student" type that inherits from "Person", I'd expect that Student to also have a name, because a Student is a Person
If a Person needs a "Name" to be constructed, then a Student would also need a "Name" to be constructed, because a Student is a Person
0

This is how you have to do it. Where would the base class get it's Http instance from if you didn't pass it in somewhere? That's just how inheritance works.

You could create separate service called ApiService or something, and in there add the functionality to send post/get requests and attaching auth tokens to each one. Then inject that into the service classes that need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.