Closed marinasundstrom closed 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")
};
Yes. I think that it is an acceptable solution.
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 ;)
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.
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...
@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
@cezarypiatek OK. I will test it soon!
An official version with this feature has been released v1.17.435
If the property type is nullable (reference type), handle and generate code that checks for null.
Like with
Image
here: