InlineMapping
InlineMapping copied to clipboard
#nullable enable
If you put #nullable enable at the top of each file then you won't need to do this:
public Destination MapToDestination(this Source source)
{
source == null ? throw new IHateYouException(nameof(source))
: new Destination {
// ...
};
}
but instead can do this:
public Destination MapToDestination(this Source source)
{
new Destination {
// ...
};
}
public Destination? MapToDestinationNullable(this Source? source)
{
source == null
? null
: new Destination {
// ...
};
}
I already put #nullable enable at the top of my generated code. Even with that, it's possible that someone could pass in a null value, and my take is that you should still check for null and throw ArgumentNullException.
One thing I would consider is some kind of configuration setting that says, "If you're given a null value, return null instead of throwing ArgumentNullException". This could be done as a setting in the attribute (that could be done as work with this issue), and/or a configuration setting.
Ah, I see I mis-read the generation code. Then throwing the YouAreADorkException here is completely the right move.
It still would be nice to allow cloning a nullable object though. Thinking further, would this work?:
Destination? destionation = source?.MapToDestination();