Search code examples
javaoopdesign-patternsservicerpc

Why transform external request class to internal request class before activity?


What's the reason for transforming the external request class to internal request class before doing CRUD works?

Example Code:

public GetResponse(ExternalRequest request) {
    InternalRequest internalRequest = RequestContextBuilder.buildRequest(request);
    InternalResponse = solve(internalRequest);
}

Could anyone tell me why does not use the external request to do the activity directly? The detail elements are same for external and internal request class.


Solution

  • Without more context it's hard to give an answer. But here are some reasons that I can imagine

    • Decoupling

      The solve method has no dependency to the ExternalRequest class. It only depends on the InternalRequest. An InternalRequest might be build using a different input then ExternalRequest. So if you can just create an InternalRequest you can invoke the solve method.

    • Validation

      Maybe the RequestContextBuilder.buildRequest does more then just creating another object. It might validate the ExternalRequest's values before creating the InternalRequest. The author might want to express that an InternalRequest has only valid values. In this case it would make the implementation of the solve method easier, because the solve method could then rely on the fact that an InternalRequest is valid. It must not check all the properties.