Closed p3t3rix closed 4 years ago
I'm not sure what's the expected behavior of this type of method. Could you provide a sample implementation?
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.
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.
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.
Just out of pure curiosity, could you provide a real-life example of chaining mapping methods?
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.
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.
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:
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 :)