Data-mapping utilities
These utilities enable you to convert from one data representation to another within an application, with a particular focus on transforming server row data. They offer a systematic approach to managing data conversion between Data Transfer Objects (DTOs) and entities, ensuring consistency and maintainability.
For more detailed information on API and configurations, please refer to the API documentation.
Key components
- ServerRowDTO Specifies the structure for server row data as exchanged with the back end. The DTO adheres to a convention of using upper-case keys.
- ServerRowEntity Represents the client-side or application-side structure for server row data, adhering to a convention of using lower-case keys.
- ServerRowDTOMapper A specialized interface extending
DTOMapper, designed specifically for mapping betweenServerRowDTOandServerRowEntity.
DTOMapper interface
The DTOMapper interface defines the core methods required for data transformation between DTOs and entities. These methods include:
fromDTO- Transforms a DTO instance into its corresponding entity representation.toDTO- Converts an entity instance back into its DTO format.
You can use these utilities to achieve a clear separation of concerns and streamline data processing workflows between back-end systems and the client application.
DefaultServerRowDTOMapper class
The DefaultServerRowDTOMapper class provides a concrete implementation of the ServerRowDTOMapper interface, delivering robust methods to handle conversions between ServerRowDTO and ServerRowEntity seamlessly.
Conversion methods
fromDTO(dto: ServerRowDTO): ServerRowEntity: Transforms aServerRowDTOinstance into aServerRowEntity. This process involves converting keys from upper-case to lower-case while maintaining data integrity.toDTO(entity: ServerRowEntity): ServerRowDTO: Converts aServerRowEntityback into itsServerRowDTOrepresentation, translating keys from lower-case to upper-case.
Usage example
Below is an example demonstrating the usage of DefaultServerRowDTOMapper in a typical application:
import { ServerRowDTOMapper } from '@genesislcap/foundation-utils';
import { GenesisElement } from '@genesislcap/web-core';
export class MyExampleComponent extends GenesisElement {
@ServerRowDTOMapper dtoMapper!: ServerRowDTOMapper; // DI token
private dtoMapper: ServerRowDTOMapper = new DefaultServerRowDTOMapper();
private serverRowDTO: ServerRowDTO = {
DETAILS: {
OPERATION: 'INSERT',
ROW_REF: '12345',
},
};
connectedCallback() {
super.connectedCallback();
const serverRowEntity = dtoMapper.fromDTO(serverRowDTO);
console.log(serverRowEntity); // Outputs the entity representation
const backToDTO = dtoMapper.toDTO(serverRowEntity);
console.log(backToDTO); // Outputs the original DTO representation
}
}
This example illustrates how to use the DefaultServerRowDTOMapper to handle seamless conversions, enabling consistent data representation across different application layers.
Considerations
- Use the mapper utilities for all server communication involving server row data to ensure consistency in data handling.
- Extend the
DTOMapperinterface for other data types as needed, maintaining a uniform approach to data conversion across your application. - Use dependency injection for accessing mapper instances, enhancing modularity and testability.