Skip to main content

Preface

Welcome to AutoMapper TypeScript! This basic tutorial will show you the problem that AutoMapper tries to solve and how to use AutoMapper to solve it.

Let's assume that you are building a method to return some User information. The application has the following models:

  • User: the data of a user from the database
  • UserDto: the shape of the user data that you want to return from the method
export class User {
firstName: string;
lastName: string;
username: string;
password: string;
bio: Bio;
}

export class Bio {
job: Job;
birthday: Date;
avatarUrl: string;
}

export class Job {
title: string;
salary: number;
}

The issues with the above approach are:

  • Entity and DTO are coupled. UserDto knows about User.
  • Mapping logic is repetitive and grows as your models grow.

Let's see how AutoMapper can help. If you just want to see the final code, skip to Summary