0

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 to 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?

Alex Herman
  • 2,708
  • 4
  • 32
  • 53
  • in one of my recent posts I've detailed the `DTO structure` https://stackoverflow.com/q/51255594/5374333 from that post - I use Approach 2 – Alex Herman Jul 10 '18 at 05:34

2 Answers2

2

There are tools that can make the coding a bit easier, such as Automapper, but they can have performance problems. In general the pattern of mapping from your data model into the domain model is the correct way to go. Having the stored procedure flatten the data in to a single row seems like it would provide the best performance. Issuing several individual queries to get the pieces and assembling them in code would suffer from the query submission overhead.

Brad Irby
  • 2,397
  • 1
  • 16
  • 26
0

If you care that much about your performance, than why the hell you code in C#? Take it wisely and do C++. Otherwise, you have no option except manual mapping as it is now. No frameworks like abovementioned Automapper are fast enough to overperform manual solutions.

The only thing is good to know is generics. You might have something like public abstract TOutput Map() where TOutput : ModelBase for example.

Zazaeil
  • 3,900
  • 2
  • 14
  • 31