A web service returns a text-based response.
How can the logic required to construct a response be reused by multiple services?
Clients can become coupled to domain layer entities even when the internal mechanisms used to fulfill requests are hidden. For example, the act of annotating Domain Model objects with data binding instructions (e.g. JAXB) and using those same objects in responses increases client-service coupling. Service developers can eliminate these dependencies by creating distinct classes that define response data structures, however, additional logic must be created to move and transform data from domain entities into these structures. This logic is frequently incorporated into the service logic, which in turn makes the service harder to maintain and often results in duplicate code.
Create a class that consolidates the data mapping and transformation logic used to create a response.
Response Mappers are a specialization of the Mapper pattern. They consolidate the data retrieval, transformation, mapping, and serialization logic used to create a specific response or family of responses. Response Mappers are often selected and instantiated directly by services, but other techniques (e.g. Inversion of Control) may be used as well. Once a mapper has been instantiated, the service calls methods on the mapper to pass domain objects, Record Sets, structs, or primitive data. At some point, the service calls the mapper to acquire a final response that is returned to the client.