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

Mapping Property Object Over Target #64

Closed
SlowLogicBoy opened this issue Jun 26, 2018 · 5 comments
Closed

Mapping Property Object Over Target #64

SlowLogicBoy opened this issue Jun 26, 2018 · 5 comments

Comments

@SlowLogicBoy
Copy link

Is there a better way to do this?:

class Statistics
{
    public float Ranking {get; set;}
    public string SomeOtherRankingStuff {get; set;}
}
class Model
{
   public string SomeOtherProperties {get;set;}
   public Statistics Statistics {get; set;}
}
class ModelDto
{
   public string SomeOtherProperties {get;set;}
   public float Ranking {get; set;}
   public string SomeOtherRankingStuff {get; set;}
}
Mapper.WhenMapping
    .From<Model>()
    .To<ModelDto>()
    .After.MappingEnds
    .Call(ctx => ctx.Source.Statistics.Map().Over(ctx.Target));

I want to map Statistics property Over ModelDto, since it doesn't have a seperate property for statistics?

@SteveWilkes SteveWilkes self-assigned this Jun 26, 2018
@SteveWilkes
Copy link
Member

Hi!

That's probably as good a way of doing it as exists currently - is there a configuration syntax or way of performing the mapping you'd like to be able to use?

Depending on your configuration-placement preferences, it's a tiny bit neater configuring it inline, imo:

var modelDto = model.Map().ToANew<ModelDto>(cfg => cfg
    .After.MappingEnds
    .Call((m, dto) => m.Statistics.Map().Over(dto)));

@SlowLogicBoy
Copy link
Author

SlowLogicBoy commented Jun 26, 2018

Hi,

Thanks for a fast response.

I was hoping to find something like

cfg => cfg.WhenMapping.From<Video>()
    .To<YoutubeVideo>()
    .Map(ctx => ctx.Source.Statistics).ToSelf()

or maybe .Map(ctx => ctx.Source.Statistics).To(t => t) but this might be difficult since arguments is an expression. As a worst case (probably) .Manual(Action<TTarget, TSource> manualFunc)
'.Manual' example:

.Manual((t, s) => t.Statistics.Map().Over(s));

@SteveWilkes
Copy link
Member

I like ToSelf(), or maybe ToTarget() - I'll look at adding it in this week.

Thanks for the feedback! :)

SteveWilkes added a commit that referenced this issue Jun 28, 2018
…et mapping, re: issue #64 / Updating type converters to structs
SteveWilkes added a commit that referenced this issue Jul 1, 2018
* Support for configuring complex type members as sources for root target mapping, re: issue #64 / Updating type converters to structs

* Adding ToTarget() configuration API method

* Catching invalid .ToTarget() API configurations

* Start of handling runtime-type-checking of configured root target source members

* Support for overwrite mapping from configured root target sources

* Short-circuiting Relative member chain creation when possible

* Increased test coverage for root target data sources - invalid configurations and values with no member matches

* Support for conditional configured root source members

* Tidying / Start of support for configured enumerable root data sources

* Replacing uses of Linq Select() and Where()

* Support for configured enumerable root source members

* Support for multiple configured root enumerable data sources

* Test coverage for configuring multiple complex type root source members and root source members inline

* Support for runtime-typed configured root source members

* Support for mapping configured root data sources to dictionaries

* Test coverage for configured root source member mapping to dynamic
@SteveWilkes
Copy link
Member

SteveWilkes commented Jul 1, 2018

Added in the latest code - release to follow:

Mapper.WhenMapping
    .From<Video>().To<YoutubeVideo>()
    .Map(ctx => ctx.Source.Statistics)
    .ToTarget();

...or:

var youtubeVideo = video.Map()
    .ToANew<YoutubeVideo>(cfg => cfg
        .Map(ctx => ctx.Source.Statistics)
        .ToTarget());

@SteveWilkes
Copy link
Member

Available in v0.24 - thanks for the feedback! :)

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

No branches or pull requests

2 participants