Supply additional parameters to the mapping method
See original GitHub issueSometimes you want to provide an external value to the mapping method. For example your DTO doesn’t have a tenant id. You are getting the tenant id from the currently signed in user and it’s the same for every entity inside the current request.
[Mapper]
public partial class CarMapper
{
public partial Car Map(CarDto dto, string tenantId);
}
public record Car(string Name, string TenantId);
public record CarDto(string Name);
Example usage:
public void UseMapper(CarMapper mapper, IEnumerable<CarDto> dtos)
{
string tenantId = ""; //GetFromDb;
var entities = dtos.Select(dto => mapper.Map(dto, tenantId));
}
You could accomplish this by moving TenantId
to a property and using the after map feature. But I think this solution is cleaner. It doesn’t force you to change your domain code for a mapper, it shows the intent more clearly and it’s simpler to add.
Issue Analytics
- State:
- Created a year ago
- Reactions:9
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Additional parameter in mapstruct mapper - java
As described in the answer, you can use @Context . Firstly, add a single object mapping method: @Maping(target = "owner", source = "owner") ......
Read more >Python - pass multiple arguments to map function
You can pass as many iterable as you like to map() function in Python. Ensure that function has one parameter for each iterable....
Read more >Using parameters in a mapping
When you create the mapping, you want to use parameters for the source connection, source object, and state. You update the parameter values...
Read more >Module 9 Advanced Parameterization & ReST Utilization
23K views · 5 years ago ... more ... Informatica Intelligent Cloud Services- IICS SCD Type 2 Mapping Create/Implementation.
Read more >Use runtime and type-safe parameters - Azure Pipelines
With runtime parameters you can: Supply different values to scripts and tasks at runtime. Control parameter types, allowed ranges, and defaults.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Fube I don’t think flattening/unflattening would be the right approach for this, it could work as a workaround though. IMO the way to go would either be providing parameters directly to the mapping methods which are then mapped by name to properties of the target object or to add a secondary source objects which are also considered as mapping sources.
@latonz I haven’t really gotten around to it yet. I made some unit tests and looked at the relevant bits and pieces. It seems I have underestimated the scope of this change. I am still interested in going forward with it, but I have to find some free time.