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

Mapping Nullable<bool> to NotNullable<bool> #96

Closed KinNeko-De closed 4 years ago

KinNeko-De commented 4 years ago

Hello. i tried your extension (Version 1.12.306) at some code. Its great but i think i have found a small issue.

I have a domain object with a nullable bool property and it should be mapped to not nullable bool dto. This is bad design.. i know that :)

The MappingGenerator generates:

private Dto ToDomain(DomainObject domainobject)
{
   return new Dto(notnullableproperty: domainobject.nullableproperty.Value);
}

This code will fail when the nullableproperty is null. I suggest to use nullableproperty.GetValueOrDefault() instead

cezarypiatek commented 4 years ago

Hi,

Current behavior was introduced intentionally. NullReferenceException should bring your attention to the discrepancy between the models . If you need a different behavior you should manually adjust generated code - that's the beauty of MappingGenerator.

KinNeko-De commented 4 years ago

Ok. I will get the atttention if i have a good unit test that does boundary tests :)

But i agree that a MappingGenerator is the best concept because you can easily change the code if the result is not what you expected.

cezarypiatek commented 4 years ago

@KinNeko-De If you are using R#, you can configure static analysis to report all these situations as an error so you don't need to write tests for these kind scenarios. Not sure if there is an existing Roslyn analyzer that can act as counterpart for this R# rule.

image