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.

Unexpected behavior for CustomValueResolver

See original GitHub issue

I’m experiencing an unexpected (for me, at least) behavior mapping a DTO to an existing Entity:

Entity ret = Mapper.Map(dto, existingEntity)

I’ve built a simple (I hope) sample that shows the behavior (using both Automapper 5.2.0 AND 6.1.0). Sample code here.
Basically, my aggregate is a master with a list of details, where both master and detail entity implements an interface that defines a property (a relation to a separate entity).
This relation is far complicated (not in the sample, of course), so I’ve defined a CustomMemberValueResolver that targets interface declaration for direct and reverse mappings.

internal class CustomMemberValueDTOResolver 
        : IMemberValueResolver<IWithNestedEntity, IWithNestedDTO, NestedEntity, NestedDTO>
{ ... } // see sample code attached
internal class CustomMemberValueEntityResolver 
        : IMemberValueResolver<IWithNestedDTO, IWithNestedEntity, NestedDTO, NestedEntity>
{ ... } // see sample code attached

Mapper.Initialize(cfg =>
{
	cfg.CreateMap<NestedEntity, NestedDTO>()
		.ReverseMap();

	cfg.CreateMap<MasterEntity, MasterDTO>()
		.ForMember(e => e.Nested,
			expr => expr.ResolveUsing<CustomMemberValueDTOResolver, NestedEntity>(e => e.Nested))
		.ReverseMap()
		.ForMember(e => e.Nested,
			expr => expr.ResolveUsing<CustomMemberValueEntityResolver, NestedDTO>(e => e.Nested));

	cfg.CreateMap<DetailEntity, DetailDTO>()
		.ForMember(e => e.Nested,
			expr => expr.ResolveUsing<CustomMemberValueDTOResolver, NestedEntity>(e => e.Nested))
		.ReverseMap()
		.ForMember(e => e.Nested,
			expr => expr.ResolveUsing<CustomMemberValueEntityResolver, NestedDTO>(e => e.Nested));
});
Mapper.AssertConfigurationIsValid();

The problem is that my resolver for the “ReverseMap” part cannot create a new entity when “DestMember” is null, because DestMember should NEVER BE NULL, since I’m mapping DTO from an existing entity and (for application logic) this code always is provided with a “non-null relation”.
What I see from test code provided is that a mapping is called from an object with a null relation, object that actually should never exists (or even better: I’m not able to get in the logic of what happens). I’ve digged also source code but actually I’m unable to see who (and where) creates this “empty” object with null relation.

I see that is difficult to explain the problem, hope that sample attached can better explain my problem. Thanks for any kind of help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, Jun 26, 2017

I’d like to get the 6.1.1 release out a bit sooner as a bugfix release for whatever we broke in 6.1.0 😃

1reaction
lbargaoanucommented, Jun 23, 2017

What can I say 😃 Try to simplify things. Start small and build to your final setup. Or try something outside your app just to see what AM does. Of course the execution plan differs, mainly because the types are different. I don’t see anything wrong with what AM does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Value Resolvers
We have several options in telling AutoMapper a custom value resolver to use, including: MapFrom<TValueResolver>; MapFrom(typeof(CustomValueResolver)); MapFrom( ...
Read more >
Error handling - Apollo GraphQL Docs
You can create a custom errors and codes using the graphql package's GraphQLError ... For example, this resolver throws a custom error if...
Read more >
Cant Create Automapper custom value resolver
I have been trying to create a automapper Custom value resolvers, but I seem to have missed some set up step as it...
Read more >
GraphQL - Resolvers
A GraphQL::Schema::Resolver is a container for field signature and resolution logic. It can be attached to a field with the resolver: keyword:.
Read more >
Get Started with Custom Error Handling in Spring Boot (Java)
Learn how to implement custom error handling logic in Spring Boot. You will see two approaches based on the @ControllerAdvice annotation.
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