Mapster认为我的Lambda表达式中的属性访问不是属性访问。

huangapple go评论58阅读模式
英文:

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?

英文:

I have two way config for mapping using Mapster:

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

Later I use it as below:

public async Task&lt;Models.ClientAggregate.Client?&gt; FindEnabledByClientIdAsync(string clientId, CancellationToken cancellationToken = default)
{
    return await _dbContext.Clients.AsNoTrackingWithIdentityResolution()
        .ProjectToType&lt;Models.ClientAggregate.Client&gt;(MappingConfigs.ClientConfig.SourceToDestinationSetter.Config)
        .Where(p =&gt; p.ClientId == clientId &amp;&amp; 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?

答案1

得分: 2

你至少应该移除TwoWays(),它会导致在两种方式(即直接映射和反向映射)中定义映射:

> 如果你需要将对象从POCO映射到DTO,然后再从DTO映射回POCO,你可以使用TwoWays一次性定义设置。

因此,生成的映射应该是:

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

dest表达式将成为反向映射的源表达式,它们显然不是成员访问表达式,因此会引发异常。

英文:

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&lt;Client, Models.ClientAggregate.Client&gt; ClientConfig =&gt;
    TypeAdapterConfig&lt;Client, Models.ClientAggregate.Client&gt;
    .NewConfig()
    .Map(dest =&gt; dest.AllowedGrantTypes, src =&gt; src.GrantTypes.Select(p =&gt; p.Type))
    .Map(dest =&gt; dest.AllowedRedirectUris, src =&gt; src.RedirectUris.Select(p =&gt; p.Uri))
    .Map(dest =&gt; dest.AllowedAuthorizationDetailSchemas, src =&gt; src.Actions.GroupBy(p =&gt; p.AuthorizationDetailSchema.Type)
        .Select(p =&gt; new Models.ClientAggregate.ClientAllowedSchema
        {
            SchemaType = p.Key,
            AllowedActions = p.Select(x =&gt; x.Name).ToList()
        }))
    .Map(dest =&gt; dest.AllowedClaims, src =&gt; src.Claims.Select(p =&gt; p.Type));

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

huangapple
  • 本文由 发表于 2023年5月11日 00:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定