0

I have an interface RequestProcessorInterface. There are different scenarios for processing a json request e.g. async vs synchronous request. I am trying to use template method pattern where the steps are like validate, preProcess, saveInDatabase, postProcess, etc


        ProcessingContext processingContext = validate(storeContentRequestContainer);
        processingContext = preProcess(storeContentRequestContainer, processingContext);
        saveInDatabase(storeContentRequestContainer, processingContext);
        return postProcess(storeContentRequestContainer, processingContext);

My ProcessingContext class has these attributes:

Map<String, String> errors;
String contentId;
String correlationId;
String presignedUrl;
String objectKey;
String bucketLocation;
DbEntity dbEntity; // Entity for database storage

When the json request is parsed, I assign values to 'processingContext' object. To keep the system flexible and not worry about what step might need the parsed information, I am encapsulating the extracted information in the context object.

Also I am passing the context object to every step so that in future, every step has this information readily available. I was going in the direction of most of the steps to be able to read the context and update some attribute and return the modified context , so the subsequent steps have access to attributes populated earlier.

I have a feeling that accepting context object (which is mutable) and modifying it and returning it is not a good idea. This context object is going to be in the method scope of a singleton class (spring boot). It will not be something lingering on forever and that should make it simpler.

How do I achieve this flexibility of multiple steps to be able to augment / update information in a context object? Will it make sense to make this context object immutable ?

makcalif
  • 37
  • 7
  • In my own experience, context object is another name for god object. Everything touches it. Every new feature modifies it. The context grows and grows. It's more work in the short term to define separate APIs for every step in the process, but less painful in the long term. – jaco0646 Dec 25 '20 at 15:50
  • The variables in your context should probably be injected instead. If they change from request to request then have a factory create your template services using the request so it can still be injected on creation. – PtrTon Dec 27 '20 at 00:29

0 Answers0