Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Possible Code-Generation for TDest Map(TSource source, TDest dest) #93

Closed
p3t3rix opened this issue Oct 7, 2019 · 7 comments
Closed

Comments

@p3t3rix
Copy link

p3t3rix commented Oct 7, 2019

Because Automapper has a similar interface method, it would be nice to generate a mapping with the following method-signature:
TDest Map(TSource source, TDest dest)

At the moment this code suggests a mapping but it looks like this:

public override MyDest Map(MySource source, MyDest target)
  {
    return new MyDest();
  }

Would it be possible to suggest a mapping generation similar to the mapping if the method wouldn't return something ? Maybe an additional suggestion or a fallback if the mapping is just the default-constructor.

p.s. thanks for this project :)

@cezarypiatek
Copy link
Owner

I'm not sure what's the expected behavior of this type of method. Could you provide a sample implementation?

@p3t3rix
Copy link
Author

p3t3rix commented Oct 7, 2019

   public MyDest Map(MySource source, MyDest target)
        {
            target.Foo = source.Foo;
            target.Bar = source.Bar;
            target.Baz = source.Baz;
            return target;
        }

Basically the same as the generated mapping for this signature

   public void Update(MySource source, MyDest target);

The reason why it would be nice to have is that the signature with return-value can be chained, used as input-parameter, etc.

@cezarypiatek
Copy link
Owner

I'm not convinced about this idea. This kind of method is very confusing. When I see that kind of signature I always need to check the implementation because it's very hard to figure out what the expected outcome should be. Besides, method chaining makes the debugging very hard. Adding this kind of feature to MappingGenerator would support bad programming practices.

@p3t3rix
Copy link
Author

p3t3rix commented Oct 7, 2019

I just wanted to recreate a seamless drop in implementation for an API that already exists that way in automapper. Not really agreeing on the bad code aspect here but ok, fair enough.

@cezarypiatek
Copy link
Owner

Just out of pure curiosity, could you provide a real-life example of chaining mapping methods?

@p3t3rix
Copy link
Author

p3t3rix commented Oct 8, 2019

It's less the chaining and more the return type that is useful, for example you have your ApiController and you can call

Ok(_mapper.Map(sourceBo, targetDto); 

But i guess i can work around the limitation by adapting my BaseMapper a bit. Thanks for your time.

@p3t3rix p3t3rix closed this as completed Oct 8, 2019
@cezarypiatek
Copy link
Owner

I would suggest naming mapping method that updates the second parameter just Update:

public void Update(MySource source, MyDest target)
{
	target.Foo = source.Foo;
	target.Bar = source.Bar;
	target.Baz = source.Baz;
}

and split the usage into two separrated lines (do not invoke method inside the parameter):

_mapper.Map(sourceBo, targetDto);
Ok(targetDto); 

In that way, your code will be more readable and easier to understand.

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

No branches or pull requests

2 participants