0

Payload:

{
    "selection": {
        "ids": [1,2,3,4,5]
    },
    "image": {
        "backgroundColor": "#FFFFFF",
        "headlineColor": "#000000",
        "format": "PNG"
    },
    "processors": [
        {
            "repository": "org.my.world.app.repositories.RepoX",    // fetch data from this Repository
            "builder": "org.my.world.app.builders.ModelBuilderA"    // use this ModelBuilder with the fetched data
        },
        {
            "repository": "org.my.world.app.repositories.RepoX",
            "builder": "org.my.world.app.builders.ModelBuilderB"
        },
        {
            "repository": "org.my.world.app.repositories.RepoY",
            "builder": "org.my.world.app.builders.ModelBuilderA"
        },
        {
            "repository": "org.my.world.app.repositories.RepoY",
            "builder": "org.my.world.app.builders.ModelBuilderB"
        }
    ]
}

This payload generates a result based on the 5 provided ids [1,2,3,4,5]. The 5 ids are fed trough each defined processor. Each processor object is a definition of how each individual id is processed. The payload is send as a POST request to a specific REST endpoint. e.g.: http://org.my.world/generate/result

Currently in the application I try to instantiate the class with the provided class path or check for an exisiting class with the given class path.

public interface Repo {
    Model readId( int id );
}

@Inject RepoX repoX;
@Inject RepoY repoY;

public Model fetchModel(id) {

    Model model = new Model();

    if ( repository.equalsIgnoreCase( RepoX.class.toString() ) ) {
        model = repoX.readId( id );
    }
    else if ( repository.equalsIgnoreCase( RepoY.class.toString() ) ) {
        model = repoY.readId( id );
    }

    return model;
}

As soon as more repositories are added, to the app, the bigger the if(switch) statement gets. Is there a better way to this? Please share your thoughts.

  • Have you tried indexing or listing repositories? – Laiv Feb 14 at 20:09
  • @Laiv could you elaborate a bit on what you are suggesting? – Stevan Feb 19 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.