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

Question - mapping multiple classes to a single one ? #184

Closed
Alysaar opened this issue Dec 30, 2019 · 5 comments
Closed

Question - mapping multiple classes to a single one ? #184

Alysaar opened this issue Dec 30, 2019 · 5 comments

Comments

@Alysaar
Copy link

Alysaar commented Dec 30, 2019

Hello,

AgileMapper has worked wonders for me when mapping a source to different destinations but now I am having trouble with the other way around.

I am trying to map two different source instances to two properties of a destination instance. Normally I would use OnTo but I am using the ThrowIfAnyMappingPlanIsIncomplete option and it seems like they are incompatible.

Here is an example : https://dotnetfiddle.net/aVzwph

Is there a better way of doing this ?

@SteveWilkes
Copy link
Member

Hello!

Glad to hear you've found the mapper useful! :)

There's been other holes found in mapping validation lately, and I think there might be some missing functionality here. This:

mapper.WhenMapping
    .From<SourceClass>().ToANew<DestinationClass>()
    .Map((source, _) => source.TheCat).To(dest => dest.PetNames)
    .And
    .Map((source, _) => source.TheDog).To(dest => dest.PetNames);

...errors because we're setting two different data sources for the same target member, and I think that's probably what it should do - maybe we should be able to do this:

mapper.WhenMapping
    .From<SourceClass>().ToANew<DestinationClass>()
    .Map((source, _) => source.TheCat).To(dest => dest.PetNames)
    .Then
    .Map((source, _) => source.TheDog).To(dest => dest.PetNames);

...with .Then indicating the two data sources are intended to be supplied sequentially, and there's been no mistake in configuring two different sources for the same member?

In the meantime, there's this:

mapper.WhenMapping
    .From<SourceClass>().ToANew<DestinationClass>()
    .Map((source, _) => source.TheCat).To(dest => dest.PetNames);

mapper.WhenMapping
    .From<SourceClass>().ToANew<PetNames>()
    .Map((source, _) => source.TheDog).ToTarget();

But that fails validation because the mapper isn't figuring out that applying TheDog to PetNames using ToTarget gives PetNames.DogName a matching source. That's a bug, and I'll sort it out.

Thanks for the feedback, and happy new year!

Steve

@Alysaar
Copy link
Author

Alysaar commented Jan 9, 2020

Thank you for your reply, I will work on a workaround on my side.

I like your .Then idea, but what would you think of doing this instead :

mapper.WhenMapping
    .From<SourceClass>().ToANew<DestinationClass>()
    .Map((source, _) => source.TheCat).To(dest => dest.PetNames)
    .And
    .Map((source, _) => source.TheDog).OnTo(dest => dest.PetNames);

This would reuse your OnTo expression, and the meaning seems similar to me.

Cheers.

@SteveWilkes
Copy link
Member

Hello again!

.ToTarget() configurations are taken into account when validating mapping plans in the latest 1.7 release branch code - preview release to follow.

I'm going to include the Then sequential mapping syntax as well - I think that'll be simpler to implement than using OnTo because switching from object creation to merging within a mapping would be pretty tricky - it also might not get the behaviour we want here, because merging checks existing values aren't populated before mapping.

Thanks again for the feedback - stay safe!

Steve

@SteveWilkes SteveWilkes added the in-branch A feature or bug fix exists in code, and a release will follow label Apr 18, 2020
SteveWilkes added a commit that referenced this issue Apr 20, 2020
* Basic sequential data source support, re: issue #184

* Erroring if duplicate sequential data sources are configured

* Extra test coverage

* Erroring if sequential data source configured for simple type members

* Extra test coverage

* Erroring if configured data sources conflict with ignored source members

* Start of API update

* Updating sequential data source API

* Support for conditional sequential data sources

* Extra test coverage

* Extending test coverage

* Adding documentation

* Extra test coverage

* Start of sequential data sources for ctor parameters / Properly using fallback values in more places / Fixing tests

* Moving map methods back into interface to fix test overload resolution?!

* Fixing data source conflict tests

* Removing sequential ctor data source test

* Updating to v1.7-preview1
@SteveWilkes
Copy link
Member

Sequential data source configuration is included in v1.7-preview1, which is now available on NuGet.

mapper.WhenMapping
    .From<SourceClass>().ToANew<DestinationClass>()
    .Map((source, _) => source.TheCat)
    .Then((source, _) => source.TheDog)
    .To(dest => dest.PetNames);

Cheers!

@SteveWilkes SteveWilkes added in-preview A feature or bug fix exists in a preview release, and a full release will follow and removed in-branch A feature or bug fix exists in code, and a release will follow labels Apr 20, 2020
SteveWilkes added a commit that referenced this issue Jun 14, 2020
* Optimising for loops!

* Tidying

* Organising enumerable mapping classes

* Tidying enumerable mapping code

* Fixing struct method creation factory caching

* Updating projects to .NET Core project format / Tidying

* Converting remaining projects to .NET Core project format, removing common AssemblyInfo files

* Adding more-efficient FilterToArray method

* Organising classes

* Adding .NET 3.5 'hint path'

* Using Environment.NewLine in test

* Updating NUnit version

* Organising configuration classes

* Updating .Net35 test project

* Trying NUnit test discovery fix

* Removing

* Removing unused code

* Updating to v1.7

* Features/map instances using (#192)

* Initial test + implementation

* Resetting implementation

* Adding mapping factories

* Continued

* Support for custom, unconditional root mapping factories

* Continued

* Removing target null checking from nested access checks

* Integrating configured mapping factories as part of alternate mappings in ComplexTypeMappingExpressionFactory

* Support for custom mapping factories in derived collection elements / Erroring if custom factories have invalid return types

* Extending test coverage

* Test coverage for configured root element mapping factory

* Support for unconditional configured enumerable mapping factories

* Support for conditional configured root enumerable mapping

* Extending test coverage

* Incorrect mapping factory configuration test coverge / Updating documentation

* Tidying

* Support for configuring non-enumerable to enumerable mapping using MapInstancesUsing

* Refactoring

* Support for root configured non-enumerable to enumerable mapping

* Extending test coverage

* Updating documentation / Adding inline mapping factory tests

* Support for configured non-enumerable to enumerable element mapping

* Fixing Net35 build

* Support for conditional configured non-enumerable to enumerable mapping (#193)

* Support for conditional configured non-enumerable to enumerable mapping

* Fixing tests

* Tidying

* Removing duplicate short-circuit returns

* Extending test coverage

* Bugs/173 dictionary values to target (#194), re: issue #173

* Splitting ToTarget DataSource tests into dedicated test class / Adding failing test to prove bug

* Fixing Dictionary.Values to List ToTarget data source mapping

* Adding .NET Core 3.1 test project + updating packages and framework runtimes

* Fix for configured simple-to-complex array data sources

* Simplifying 'invocation position' code

* Setting ConfiguredLambdaInfo target value factory in ctor

* Features/value injection (#195)

* All tests passing using ValueInjectors!

* Removing ParametersSwapper + tidying

* Deferring creation of Create mapping data calls

* Support for same-type enumerable-to-enumerable to-target data sources

* Including configured ToTarget data sources in mapping plan validation, re: #184

* Tidying

* Updating readme

* Features/sequential data sources (#196)

* Basic sequential data source support, re: issue #184

* Erroring if duplicate sequential data sources are configured

* Extra test coverage

* Erroring if sequential data source configured for simple type members

* Extra test coverage

* Erroring if configured data sources conflict with ignored source members

* Start of API update

* Updating sequential data source API

* Support for conditional sequential data sources

* Extra test coverage

* Extending test coverage

* Adding documentation

* Extra test coverage

* Start of sequential data sources for ctor parameters / Properly using fallback values in more places / Fixing tests

* Moving map methods back into interface to fix test overload resolution?!

* Fixing data source conflict tests

* Removing sequential ctor data source test

* Updating to v1.7-preview1

* Updating release notes, adding v1.7-preview1 NuGet package

* Surfacing mapping plan Expressions / Updating to ReadableExpressions v2.4

* Adding icon files

* Updating packages

* Features/to target instead (#198)

* Start of ToTargetInstead logic

* Refactoring

* Moar

* Ensuring configured mappings include callbacks and exception handling

* Extending ToTargetInstead test coverage / ToTargetInstead support with simple type members

* ToTargetInstead support for factory Funcs

* Extending test coverage

* Extending test coverage

* Erroring if simple ToTarget source configured for complex target

* Conditional ToTargetInstead support for complex type enumerable elements

* Tidying

* Tidying

* Extra test coverage

* Updating documentation

* Erroring with redundant ToTarget configurations

* Updating .NET Core framework versions for AppVeyor

* Adding Test framework reference for AppVeyor

* Updating framework version for AppVeyor

* Adding NuGet package v1.7
@SteveWilkes
Copy link
Member

v1.7 is now available on NuGet.

Thanks again!

@SteveWilkes SteveWilkes removed the in-preview A feature or bug fix exists in a preview release, and a full release will follow label Jun 14, 2020
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