Skip to content

Enhance Automapper functionality #78

@thoma01

Description

@thoma01

Hey @Ulimo !

Got a great idea to improve the functionality of AutoMapper!

Currently you allow for AutoMapper to map a bool value to a model, but what about if the policy decision would decide if the source value is mapped to the destination?

Example:

opt.CreateMap<TodoModels.TodoItem, TodoModels.TodoItemView>(AutoMapper.MemberList.Destination)
        .ForMember(x => x.Note, opt => opt.MapFromPolicy("todoitem.note", src => src.Note));

This way the policy evaluates if the source is mapped to the destination. This would allow for policy mapping per cell in a table of results. The desire is to not need to have a CanViewNote bool that would be mapped from policy that then needs to be applied to the value of Note.

I have worked on some changes that I think get this started, but it seems there is some more logic that you might already understand to help figure out what is going on. We would like to have all these conditions apply to the Queryable for as long as possible, and not have to do (as much) post-processing.

With the bool scenario you have in your samples, the generated SQL looks similar to

SELECT CASE
    WHEN (N'Thomas' = [t].[Owner]) AND ([t].[Owner] IS NOT NULL) 
    THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END AS [CanDelete], [t].[Id], [t].[Name], [t].[Owner], [u].[Username], [u].[Manager], [u].[Password]
FROM [TodoItems] AS [t]
LEFT JOIN [Users] AS [u] ON [t].[Owner] = [u].[Username]

Where you can see that the CASE statement is extremely powerful! We want to simply have this functionality but for any field type.
Example generated SQL

SELECT CASE
    WHEN (N'Thomas' = [t].[Owner]) AND ([t].[Owner] IS NOT NULL)
    THEN [t].[Note]
    ELSE NULL   -- or N''
END AS [Note], [t].[Id], [t].[Name], [t].[Owner], [u].[Username], [u].[Manager], [u].[Password]
FROM [TodoItems] AS [t]
LEFT JOIN [Users] AS [u] ON [t].[Owner] = [u].[Username]
WHERE (N'Thomas' = [t].[Owner]) OR (N'Thomas' = [u].[Manager])

Do you think this is possible and would you be open to working on this?

Also, could we upgrade the Automapper dependencies to more recent releases?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions