In my service class I would like to connect to external API. Where and how should I do it?

1) Inject in constructor ExternalClass and assign to private property. Next in other property in constructor call connect.

public MyClass(ExternalApi externalApi) {
    this.externalApi = externalApi;
    this.ExternalApiConnect externalApiConnect = externalApi.connect;
}    

public void firstMethod() {
   externalApiConnect.first();
}

public void secondMethod() {
   externalApiConnect.second();
}

2) Outside constructor, so if I build dependencies in DI, then I don't call connection to external service.

private ExternalApiConnect externalApiConnect;

public MyClass(ExternalApi externalApi) {
    this.externalApi = externalApi;

}    

public void firstMethod() {
   if (externalApiConnect == null) {
        externalApiConnect = externalApi.getConnect();
   }
   externalApiConnect.first();
}

public void secondMethod() {
   if (externalApiConnect == null) {
        externalApiConnect = externalApi.getConnect();
   }
   externalApiConnect.second();
}

But now I have two repetitions and I have state in my service class. I don't think the state should be in service class?

How is the best way for this?

Edit:

3)

public MyClass(ExternalApi externalApi) {
    this.externalApi = externalApi;
}    

public void firstMethod() {
   externalApi.getConnect().first();
}

public void secondMethod() {
   externalApi.getConnect().second();
}
up vote 0 down vote accepted

First of all, you have a smell in your code, you shouldn't ever access a property directly like this externalApi.connect in your ExternalApi class, change the visibility of your instance variable connect, from public to private and create a public getter function to access it.

Secondly, if you only need the object connect in your class, you don't have to pass ExternalApi, inject only ExternalApiConnect.

In case you need both, remove the externalApiConnect variable from your class, it is already apart of the externalApi state, why duplicate references to an object...

  • Thanks, but what with constructor call and connect with external service in this case? I don't want to connect to external service if I build dependencies. – maxu Nov 10 at 18:30
  • @maxu I don't see the relation between building dependencies and connection to an external service, please, could you clarify this point ? – isqo Nov 10 at 18:36
  • If I build dependencies, the class's constructor is called. – maxu Nov 10 at 18:52
  • @maxu, so you don't have to connect to your api in the constructor, just pass the connection object to your constructor, store it in a variable and use it everywhere or create another function in externalApi that connects to your api and returns the object, you could also use a pool of connection if your concern is to use just a limited number of connections. – isqo Nov 10 at 19:33
  • 1
    I added 3 example. It is the best way? – maxu Nov 10 at 21:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.