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 members in empty target class #128

Closed mrdnote closed 3 years ago

mrdnote commented 4 years ago

Hi, thanks for this wonderful time-saving tool. It's my new favorite VS extension!

Another boring coding job I find myself repeatidly doing is writing out DTO classes which many time have the same members as the entity or model class were filling it from.

Wouldn't it be great if MappingGenerator would generate members, in case the target class is empty?

For example: image

In this case ListGameDto would be an emtpy class and MappingGenerator would create the Id, Title etc. members.

cezarypiatek commented 4 years ago

Hi,

I think generating fields for empty types don't add any particular value because you can easily achieve that by copy&paste of type content definition. More interesting would be the option of adding missing fields even if the target already has some (removing differences between types). However, this won't work if target type is not defined in the source code. I will mark this issue as an enhancement but I can't promise when this will be implemented.

mrdnote commented 4 years ago

I don't think copy&paste works very well on generated EF entities like this:

    /// <summary>
    /// Id of the game
    /// </summary>
    public virtual int Id
    {
        get;
        set;
    }

    /// <summary>
    /// Title of the game
    /// </summary>
    public virtual string Title
    {
        get;
        set;
    }

    /// <summary>
    /// Start date/time of the game
    /// </summary>
    public virtual global::System.Nullable<System.DateTime> StartDate
    {
        get;
        set;
    }

And for generated SQL-2-Linq it's even worse...

cezarypiatek commented 4 years ago

Thanks for bringing this example.

cezarypiatek commented 4 years ago

@mrdnote Maybe in the meantime you will be interested with my another project that seems to solve the problem of synchronizing members between different types. Everything described in my blogpost https://cezarypiatek.github.io/post/csharp-twin-types/#the-solution-extending-c-rules-with-custom-analyzer

cezarypiatek commented 3 years ago

I decided that this case if out of the scoped of MappingGenerator project. I think you can resolved it by using MappingGenerator + aftermentioned TwinType attribute