英文:
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<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?
答案1
得分: 2
你至少应该移除TwoWays(),它会导致在两种方式(即直接映射和反向映射)中定义映射:
> 如果你需要将对象从POCO映射到DTO,然后再从DTO映射回POCO,你可以使用TwoWays一次性定义设置。
因此,生成的映射应该是:
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));
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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论