lundi 9 juillet 2018

design pattern to convert one DTO into another

I'm new to design patterns and wonder what is the best way to go about converting one DTO into another.

In order to increase performance to the highest extent I use stored procedure which joins several tables (e.g. entries, definitions, examples, vocabularies) and returns the full set of data with over 40 columns - this data is required to re-create a compound DTO.

So I end up having a separate DTO (GetEntryDbDto) with over 40 fields and I manually map response from DB into it.

The second DTO is structured so it has nested collections.

Right now I keep the code that converts one DTO into another in a factory.

public class EntryFactory
{
    public static EntryDto Get(DbDataReader reader)
    {
        List<GetEntryDbDto> dbDtos = GetEntryDbDtoFactory.Get(reader);
        ...

        return entryDto;
    }
    ...

The object structure is as follows:

EntryDto contains collection of Definitions - DefinitionDto[].

Each DefinitionDto has Examples collection - ExampleDto[].


I haven't introduced any interfaces for the two above-stated classes, because they don't share common properties.

Is there a better way to refactor it using different design pattern?

Aucun commentaire:

Enregistrer un commentaire