cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

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

Closed p3t3rix closed 4 years ago

p3t3rix commented 4 years ago

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 commented 4 years ago

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

p3t3rix commented 4 years ago
   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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

p3t3rix commented 4 years ago

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.

cezarypiatek commented 4 years ago

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.