JasonBock / InlineMapping

Using the Compiler API for object mapping
MIT License
64 stars 11 forks source link

#nullable enable #27

Open robrich opened 3 years ago

robrich commented 3 years ago

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 {
      // ...
    };
}
JasonBock commented 3 years ago

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.

robrich commented 3 years ago

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();