-1

I have two versions of an API with similar but slightly different response structure. We are currently using V1 of the API and will gradually move to V2 soon. The switch has to done using a feature flag that gets triggered remotely.

Our class look like this,

@RequiredArgsConstructor
class ServiceA {

    private final ExtService<ResponseV1> extService;

    public void someMethod() {

      ResponseV1 response = extService.get(someId);
      response.updateSomeFields();
      extService.save(response);
      ......

    }
}

With feature flag, the code will look something like this.

@RequiredArgsConstructor
class ServiceA {

    private final ExtService<ResponseV1> extServicev1;
    private final ExtService<ResponseV2> extServicev2;
    private final FeatureFlagService ffService;

    public void someMethod() {

      if(ffService.useV2()) {
        ResponseV2 response = extService2.get(someId);
        response.updateSomeFields();
        extService2.save(response);
      } else {  
        ResponseV1 response = extService1.get(someId);
        response.updateSomeFields();
        extService1.save(response);
       }

      ......

    }
}

Is there any way where we can implement this where the version switching using feature flag can be abstracted? Is there any design pattern available to solve similar problem?

-Edit--------------------------------------------------- Apologies if my question was unclear. What I meant was if there any way where we can abstract the logic to switch between versions and just execute the steps without knowing which version is getting used.

8
  • Can you define what you mean by API? Is this a straight Java API, or a web API? Commented Sep 27, 2022 at 11:29
  • 1
    Note that, you are not implementing a feature flag here. Just a hot swap between externalServiceA and B... As is, you could extend this beyond the flag value range (true or false | on or off | enabled or disabled | visible or hidden).
    – Laiv
    Commented Sep 27, 2022 at 14:58
  • @GregBurghardt it's web API
    – Hindol Dey
    Commented Sep 28, 2022 at 3:53
  • 1
    Does ServiceA require any parameters or declare any return types from either of the external services? Your question just has example names with a contrived use case, so it is a little hard to answer your question. Commented Sep 28, 2022 at 12:11
  • 1
    I don't understand the question. Your feature flag code looks ok to me, but obviously not to you . Can you explain what you mean by "where the version switching using feature flag can be abstracted"? Or give an example?
    – Doc Brown
    Commented Sep 28, 2022 at 19:02

1 Answer 1

0

Feature flags are very much preferable. I don't really care what version it is, as a developer I care about the features. So feature X can be turned off in version 1.1 and turned on again in version 1.2, and by checking for the feature I don't have to change my version checking code.

Of course you can have a function or method which checks whether a feature is present, and have version checking code in that method - and nowhere else. It also makes it possible to easily have a feature available for some customers only. If customer X paid $$$ to make a feature available in the next release, you want it only there for that customer in that release - a single function returning a feature flag handles that easily.

Your Answer

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

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