Replies: 2 comments 2 replies
-
The problem is that you can have a custom map and that one can return null whenever. And there are also the built-in settings that affect the result, |
Beta Was this translation helpful? Give feedback.
-
Hmm, of course you are right. |
Beta Was this translation helpful? Give feedback.
-
Hi, hope you are well.
I've been playing around with the preview build (because of issues with the Include/IncludeBase being fixed there and not in the master) and found that in commit 7bd1b81 nullable annotations were enabled for the IMapper interfaces.
This is great, however, I feel the implementation could be smarter using some of the static analysis annotations.
Currently, using the preview package would generate loads of nullability warnings in my code. The code mainly uses the method
TDestination? Map<TDestination>(object? source);
This doesn't seem entirely correct to me, as I make sure that the type going in is not null in these cases and expect a non-null output.
Assumptions (based on my experience):
AND
THEN
The output TDestination cannot be null.
if the typeMap is not well-configured I expect an exception to be thrown, correct?
So we could solve this here by using the following:
For the generic variants I'm not sure what you would prefer. The commit made it into the following:
But then, if I use it like this:
I will still get a nullability warning on the
viewModels.Add(mapped)
.Here, I personally would expect nullability to be governed by the nullability of the generic parameters in the signature of the method, like so:
TDestination Map<TSource, TDestination>(TSource source);
This would allow for usage like above without nullability warnings, and properly warn you if you are potentially sending a null value into the mapper where you do not intend it. You could also explicitly enable nullability for the mapper like so:
Alternatively, NotNullIfNotNull could also help here, but feels less flexible to me.
I could go on, but you get the point. Of course, I am willing to open up a PR so we can discuss this further.
What do you think?
Kind regards.
Beta Was this translation helpful? Give feedback.
All reactions