Skip to content

Easy Map for properties with the same name. #582

Open
@TA50

Description

Is your feature request related to a problem? Please describe.

No response

Describe the solution you'd like

Problem

Let's assume we have the following classes:

class UserDto {
    id: number;
    email: string;
    name: string;
}

class User {
    id: number;
    email: string;
    name: string;
    password: string;
}

And we would like to create a simple mapping from User to UserDto. As often we will use a lot of properties with the same name in the entity and the dto. For now, the solution is:

createMap(mapper,
    User,
    UserDto,
    forMember(d => d.name, mapFrom(s => s.name)),
    forMember(d => d.email, mapFrom(s => s.email)),
    forMember(d => d.id, mapFrom(s => s.id)),
    // As you can see, the password is not mapped
);

Mapping properties between classes, such as User and UserDto, in JavaScript is currently a manual and tedious process. It involves repetitive work, especially when dealing with numerous properties, and can become challenging when new properties are added. Unlike AutoMapper in C#, which automatically maps properties with the same names, JavaScript lacks this capability due to the inability to extract property names from classes.

The primary problem is to simplify and automate property mapping in JavaScript, similar to AutoMapper in C#, by automatically mapping properties with matching names unless explicitly ignored.

Solution

To streamline the configuration and enable the automatic mapping of properties with matching names, I suggest the implementation of shortcut functions. These functions serve as efficient ways to configure the mapping process manually.

copySingleProperty function:

createMap(mapper,
    User,
    UserDto,
    copySingleProperty("id"),
    copySingleProperty("name"),
    copySingleProperty("email"),
);

copyManyProperties function:

createMap(mapper,
    User,
    UserDto,
    copyManyProperties('id', 'name', 'email'),
    // As you can see, the password is not mapped
);

This approach offers a more concise and manageable method for mapping properties. Although achieving automatic mapping identical to AutoMapper in C# may not be feasible in JavaScript due to the inability to extract property names from classes, these functions significantly enhance the mapping process and reduce repetitive tasks.

By employing this technique, developers have the flexibility to add properties as a list. This list can be created either manually or by utilizing a proxy object to retrieve the property names.

Here's a basic example of code for the copySingleProperty function:

function copySingleProperty<TSource, TDestination>(
    propertyName: string & keyof TSource & keyof TDestination,
): MappingConfiguration<TSource, TDestination> {
    return (mapping) => {
        const memberPath = [propertyName];
        const mappingProperty: MappingProperty<TSource, TDestination> = [
            memberPath,
            [mapFrom((source: TSource) => source[propertyName])],
        ];
        mapping[MappingClassId.customProperties].push([
            memberPath,
            mappingProperty,
            undefined,
        ]);
    };
}

Furthermore, consider making utility functions available for developers to simplify the creation of their custom configurations. These utilities, such as getMember, getMemberPath, and others, can facilitate the process of defining individualized mapping configurations.

Describe alternatives you've considered

No response

Additional context

Dependencies:

  • @automapper/classes (Version 8.7.7)
  • @automapper/core (Version 8.7.7)
  • typescript (Version 5.2.2)
  • tsc (Version 2.0.4)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions