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

Generate as extension methods? #43

Closed aeslinger0 closed 5 years ago

aeslinger0 commented 5 years ago

Looks like a great tool. I think a good enhancement would be to generate the mapping code as an extension method.

userEntity.ToUserDTO()

cezarypiatek commented 5 years ago

Hi

Thanks for suggestion. I have a few questions on start: where the extension method should be added? Should I generate a separated static class? How the class should be named?

aeslinger0 commented 5 years ago

Actually, I should be able to use your first example to generate the extension method body and then I can just add this before the parameter manually.

Or maybe your tool already supports it? I'd start with something like this:

    public static class WidgetMap
    {
        public static WidgetEntity ToWidgetEntity(this WidgetModel model)
        {
            throw new NotImplementedException();
        }
    }

And then just run your tool as normal. But I'm not able to get your extension to work at all. The lightbulb icon doesn't have your menu item in it. I'm using VS Community 2017. Is that supported? Do I need to be targeting a certain framework?

cezarypiatek commented 5 years ago

I've just tested it on extension method and it works. I'm using Visual Studio 15.8.4. Make sure that you restarted VS after extension installation. Community version should be supported.

image

Please let me know if the restart didn't help (the concrete version of VS would be helpful)

aeslinger0 commented 5 years ago

I tried uninstalling and reinstalling the extension, restarting VS, and rebooting my computer. I was using VS 15.5.1. I updated to 15.8.4 and now it works! Not sure if it was that specific version of VS that wasn't working or if something got corrupted and updating fixed it. Either way, I'm ready to start killing off AutoMapper! I'll let you know if I run into any issues with the extension. Thanks!

cezarypiatek commented 5 years ago

As I remember there was an Update of VS which broke the roslyn analyzers, probably I was 15.5.1 I'm glad that you finally managed to make it work.