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

Pass additional parameters to property mapper methods #1472

Open
Demivan opened this issue Aug 31, 2024 · 1 comment
Open

Pass additional parameters to property mapper methods #1472

Demivan opened this issue Aug 31, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Demivan
Copy link

Demivan commented Aug 31, 2024

Is your feature request related to a problem? Please describe.
I'm trying to migrate this AutoMapper code to Mapperly:

.ForMember(
     blog => blog.IsLiked,
     cfg => cfg.MapFrom(record => record.BlogLikeRecord.Any(bl => bl.UserId == currentUserId)));

This currently does not work.

[Mapper(AutoUserMappings = false)]
public static partial class BlogsMapper
{
    [MapPropertyFromSource(nameof(BlogEntry.IsLiked), Use = nameof(GetIsLiked))]
    public static partial BlogEntry MapToBlogEntry(BlogEntryRecord record, int currentUserId);

    public static partial IQueryable<BlogEntry> ProjectToBlogEntry(this IQueryable<BlogEntryRecord> query, int currentUserId);

    private static bool GetIsLiked(BlogEntryRecord record, int currentUserId) => record.BlogLikeRecord.Any(bl => bl.UserId == currentUserId);
}

Describe the solution you'd like
When parametes name and type match, pass additional parameters from Map/Project method to a property mapping methods.

// <auto-generated />
#nullable enable
namespace Blogs
{
    public static partial class BlogsMapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.0.0.0")]
        public static partial global::Blogs.BlogEntry MapToBlogEntry(global::Blogs.BlogEntryRecord record, int currentUserId)
        {
            var target = new global::Blogs.BlogEntry();
            target.IsLiked = GetIsLiked(record, currentUserId);
            return target;
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.0.0.0")]
        public static partial global::System.Linq.IQueryable<global::Blogs.BlogEntry> ProjectToBlogEntry(this global::System.Linq.IQueryable<global::Blogs.BlogEntryRecord> query, int currentUserId)
        {
#nullable disable
            return System.Linq.Queryable.Select(
                query,
                x => new global::Blogs.BlogEntry()
                {
                    IsLiked = x.BlogLikeRecord.Any(bl => bl.UserId == currentUserId)
                }
            );
#nullable enable
        }
    }
}

Describe alternatives you've considered
Move this logic outside of a mapper could be a solution. But, I think, Mapperly should be able to support this.

@Demivan Demivan added the enhancement New feature or request label Aug 31, 2024
@Demivan
Copy link
Author

Demivan commented Aug 31, 2024

And there seems to be a bug that IQueryable projections do not work with additional parameters.

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

No branches or pull requests

1 participant