I have a bunch of extension methods that convert my entities to DTOs like the following.
public static LocationDto? ToDto(this Location? @this)
{
if (@this == null) return null;
return new LocationDto
{
Id = @this.Id,
Text = @this.Name,
ParentId = @this?.Parent?.Id,
};
}
The issue here is that if you pass not nullable entity still receive a nullable and you cannot define a public static LocationDto ToDto(this Location @this) because they would be compiled to the same method.
Also, I don't like to use ! for the time I am calling it. So the following is not my answer.
Location entity = AMethod();
LocationDto dto = entity.ToDto()!;
Is there an attribute or syntax to tell the compiler how this method behaves? Somehting like:
public static [NullableOnlyIfInputIsNull] LocationDto? ToDto(this Location? @this)