Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapToTargetWith not consistent with how MapWith works #100

Closed
eborn opened this issue Feb 1, 2017 · 4 comments
Closed

MapToTargetWith not consistent with how MapWith works #100

eborn opened this issue Feb 1, 2017 · 4 comments

Comments

@eborn
Copy link

eborn commented Feb 1, 2017

I am trying to ("deep") map two classes. Please see the attached code sample:

private static void Main()
{
    var a = new Foo { A = 1 };
    var b = new Bar { A = 2 };

    //This will work as expected => b.A will be 5
    //TypeAdapterConfig<double, double>.NewConfig().MapWith(x => 5);
    //a.Adapt(b);

    //This will not work as expected => b.A will be 1, ignoring the mapping defined
    TypeAdapterConfig<double, double>.NewConfig().MapToTargetWith((x, y) => 5);
    a.Adapt(b);
}

internal class Foo
{
    public double A { get; set; }
}

internal class Bar
{
    public double A { get; set; }
}

Basically, I have no way of defining that whenever I want to for example map two doubles, I'd like to multiply the source and destination values. This came up as I tried to go around the bug with multi-dimensional arrays (see my other issue) - it always resulted in the CollectionAdapter being used even if I defined some custom mapping.

@chaowlert
Copy link
Collaborator

Hi,

You should use Map instead.
MapWith and MapToTargetWith should be used with POCO instead of primitive type.

TypeAdaperConfig<Foo, Bar>.NewConfig().Map(bar => bar.A, foo => 5);

@eborn
Copy link
Author

eborn commented Feb 14, 2017

Hello,

The way you suggest it is not a workaround to the MapToTargetWith. Let's say I have 10 classes with 100 double type properties in each. If MapToTargetWith worked correctly (the same way MapWith does), I could just define the mapping for the double type (let's say I want to always do (src,target)=>src+target when mapping). With your method, as far as I understand, I have to write 100*[number of mappings] of identical lines.

Otherwise, maybe you should make MapToTargetWith internal not to confuse users?

@chaowlert
Copy link
Collaborator

I plan to create rule base configuration to allow users to map by convention rather than map individual property. Problem is I really have no time now. For now, pls find the workaround.

For MapToTargetWith, actually it might be bug, it should work as you expect.

@chaowlert chaowlert reopened this Feb 14, 2017
@chaowlert
Copy link
Collaborator

In Mapster 3.0, MapToTargetWith now should work with your test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants