Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Mapping #871

Merged
merged 1 commit into from
Jun 25, 2023
Merged

Simplify Mapping #871

merged 1 commit into from
Jun 25, 2023

Conversation

jasontaylordev
Copy link
Owner

Overview

Simplified the object-object mapping approach by removing IMapFrom and associated MappingProfile. Mappings can now be specified using a standard AutoMapper.Profile as nested classes or in separate files as appropriate.

Motivation

The previous approach was becoming cumbersome to maintain and didn't align well with our project requirements. Removing IMapFrom and the associated MappingProfile will streamline the mapping process and improve code clarity.

Changes Made

  • Removed IMapFrom and associated MappingProfile.
  • Introduced the use of AutoMapper.Profile as nested classes or in separate files for specifying mappings.
  • Updated the usage of AutoMapper to closely follow the AutoMapper Usage Guidelines.

Example

Here's an example of the new mapping approach:

public class TodoItemDto
{
    public int Id { get; init; }

    public string? Title { get; init; }

    public bool Done { get; init; }

    public int Priority { get; init; }

    private class Mapping : Profile
    {
        public Mapping()
        {
            CreateMap<TodoItem, TodoItemDto>().ForMember(d => d.Priority, 
                opt => opt.MapFrom(s => (int)s.Priority));
        }
    }
}

@acesteb
Copy link

acesteb commented Jun 24, 2023

If I'm not mistaken, Mapping profile was only being referenced by the tests? i.e Using MappingProfile for DI registrations was removed in a previous PR. If so, that feels like a low hanging fruit with low impact.

Removing IMapFrom in favor of the nested class looks looks like the bigger impact and a step in the right direction imo. Long gone are the days of IHaveCustomMapping and IMapFrom. They served well 😅

I guess the only "downside" is the need to explicitly call 'CreateMap' even if there are no custom mappings

Edit: It looks like it's still referenced during DI registration via assembly scanning. Nonetheless, my comment still stands

@jasontaylordev
Copy link
Owner Author

I guess the only "downside" is the need to explicitly call 'CreateMap' even if there are no custom mappings

Agreed, it was nice to just specify the interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants