Szyszka947
Szyszka947

Reputation: 912

Mapster thinks my property access in lambda is not property access

I have two way config for mapping using Mapster:

public static TwoWaysTypeAdapterSetter<Client, Models.ClientAggregate.Client> ClientConfig =>
    TypeAdapterConfig<Client, Models.ClientAggregate.Client>
    .NewConfig()
    .TwoWays()
    .Map(dest => dest.AllowedGrantTypes, src => src.GrantTypes.Select(p => p.Type))
    .Map(dest => dest.AllowedRedirectUris, src => src.RedirectUris.Select(p => p.Uri))
    .Map(dest => dest.AllowedAuthorizationDetailSchemas, src => src.Actions.GroupBy(p => p.AuthorizationDetailSchema.Type)
        .Select(p => new Models.ClientAggregate.ClientAllowedSchema
        {
            SchemaType = p.Key,
            AllowedActions = p.Select(x => x.Name).ToList()
        }))
    .Map(dest => dest.AllowedClaims, src => src.Claims.Select(p => p.Type));

Later I use it as below:

public async Task<Models.ClientAggregate.Client?> FindEnabledByClientIdAsync(string clientId, CancellationToken cancellationToken = default)
{
    return await _dbContext.Clients.AsNoTrackingWithIdentityResolution()
        .ProjectToType<Models.ClientAggregate.Client>(MappingConfigs.ClientConfig.SourceToDestinationSetter.Config)
        .Where(p => p.ClientId == clientId && p.Enabled)
        .SingleOrDefaultAsync(cancellationToken);
}

And don't know why, I get the exception:

ArgumentException: Allow only member access (eg. obj => obj.Child.Name) (Parameter 'lambda')

You see that all of my destinations in mapping are property accessors. What causes the exception?

Upvotes: 0

Views: 764

Answers (1)

Guru Stron
Guru Stron

Reputation: 143511

You should at least remove TwoWays() which literally results in defining mapping in two ways (i.e. both direct and reverse mapping):

If you need to map object from POCO to DTO, and map back from DTO to POCO. You can define the setting once by using TwoWays.

So the resulting mapping should be:

public static TwoWaysTypeAdapterSetter<Client, Models.ClientAggregate.Client> ClientConfig =>
    TypeAdapterConfig<Client, Models.ClientAggregate.Client>
    .NewConfig()
    .Map(dest => dest.AllowedGrantTypes, src => src.GrantTypes.Select(p => p.Type))
    .Map(dest => dest.AllowedRedirectUris, src => src.RedirectUris.Select(p => p.Uri))
    .Map(dest => dest.AllowedAuthorizationDetailSchemas, src => src.Actions.GroupBy(p => p.AuthorizationDetailSchema.Type)
        .Select(p => new Models.ClientAggregate.ClientAllowedSchema
        {
            SchemaType = p.Key,
            AllowedActions = p.Select(x => x.Name).ToList()
        }))
    .Map(dest => dest.AllowedClaims, src => src.Claims.Select(p => p.Type));

The dest expressions will become source ones for reverse mapping, and they are clearly not a member access expressions, hence the exception.

Upvotes: 2

Related Questions