question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Supply additional parameters to the mapping method

See original GitHub issue

Sometimes 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:open
  • Created a year ago
  • Reactions:9
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
latonzcommented, Mar 29, 2023

@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.

5reactions
DerMavecommented, Jan 10, 2023

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found