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

Handle nullable (reference) types #133

Closed marinasundstrom closed 4 years ago

marinasundstrom commented 4 years ago

If the property type is nullable (reference type), handle and generate code that checks for null.

Like with Image here:

            return new Person()
            {
                Id = model.Id,
                Name = model.Name,
                Image = model.Image != null ? new ImageDto()
                {
                    Id = model.Image.Id,
                    Path = model.Image.Path,
                    Description = model.Image.Description,
                } : null
            };
cezarypiatek commented 4 years ago

How should I handle it when the destination is not nullable? Should I generate throw expression?

  return new Person()
            {
                Id = model.Id,
                Name = model.Name,
                Image = model.Image != null ? new ImageDto()
                {
                    Id = model.Image.Id,
                    Path = model.Image.Path,
                    Description = model.Image.Description,
                } : throw new NullArgumentException("model.Image")
            };
marinasundstrom commented 4 years ago

Yes. I think that it is an acceptable solution.

cezarypiatek commented 4 years ago

After a preliminary assessment, it looks like a quite complex feature. I started working on it but I can't promise when it will be delivered. In the meantime, please consider supporting MappingGenerator project ;) image

marinasundstrom commented 4 years ago

Well. I have bought you a coffee. 🙂 After having used AutoMapper lately, I feel direct code mappings is far more maintainable since it .

When I wrote this issue I was about to write a proposal on how to implement this but I opted not to.

You have to take the <Nullable>enable</Nullable> flag into consideration when generating the code.

Of the target is nullable (implicitly or explicitly with the flag) then there is no problem assigning from what could hold a null value.

The source and target types must be compatible in their nullability.

What you want to generate is code that does not yield any warning or errors due to nullable.

cezarypiatek commented 4 years ago

I think that <Nullable>enable</Nullable> MSBuild flag doesn't matter here. The generated code should be shaped by nullability annotations (if they are available). The problem is a little bit more complex because I need to track all property accessors and apply additionally conditional accessor .? if it's needed. I'm in the middle of the implementation...

cezarypiatek commented 4 years ago

@robertsundstrom Here's a pre-release version with the requested feature. Would you mind to test it and let me know if it works as expected? https://ci.appveyor.com/api/buildjobs/klonxsb0yjs8m3mi/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

A sample preview of generated code https://github.com/cezarypiatek/MappingGenerator/blob/22d25ecc3d02d35ab4b4e7224d22743016b1e12d/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/036_PureMappingMethodWithNullable_FIXED.txt

marinasundstrom commented 4 years ago

@cezarypiatek OK. I will test it soon!

cezarypiatek commented 4 years ago

An official version with this feature has been released v1.17.435