[Proposal] Parameter Validation Source Generator Attributes #6373
Replies: 5 comments 3 replies
-
The use of attributes to drive this validation had been discussed during design meetings and wasn't preferred for a couple of reasons. One is that it's quite verbose, at least compared to the alternatives. Another is that it causes attributes to change the behavior of the code as it is written, which is something attributes don't (or rarely) do today. Lastly is that these attributes already exist in the BCL and do not imply this behavior, so it'd be impossible to use the already existing attributes without causing breaking changes, so a duplicate set of attributes would have to be introduced, which isn't exactly an ideal circumstance. The team has also expressed that they are not interested in using source generators for AOP scenarios, like validation. The issue seemed to be mostly that there is behavior silently inserted into the code of an existing method which would make it a challenge to understand what the method does. I personally do not agree with this sentiment as annotation-driven AOP is somewhat common in Java and is not a source of confusion. Perhaps parameter validation is a simple enough case to not cause this concern, but I'm of the opinion that a narrow implementation of AOP via source generators is not all that worthwhile. I'm kind of just hoping that |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Thank you for your detailed reply. Yes I also hope that !! is reconsidered... I mean it works in swift.. Regarding SourceGeneration I am happy we have the same opinion - It works in Java. To make it distinguishable maybe the Ide could highlight these source modifying attributes somehow. (Also maybe there could be SourceGeneratorAttribute base class to clearly Identify?) I would not want a new a new language concept for that like attributes with () to indicate source generation MyMethod((NotNull) Citizen citizen) |
Beta Was this translation helpful? Give feedback.
-
if the concern is "undefined extra behavior in the class" from attributes that trigger source generation |
Beta Was this translation helpful? Give feedback.
-
What you're suggesting is basically attributes that specify constraints for method contracts, as was majorly mentioned against the introduction of the |
Beta Was this translation helpful? Give feedback.
-
Yes and by using attributes it is also clear to the outside what kind of "contract" a method has and what parameter values are expected. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It is quite sad the !! operator did not make it into C# but also understand that it was quite specific and did not cover all cases.
But since the !! just generated code, how about we allow source generators for parameter validation
so everybody can build their own validation, with very readable attributes.
Usage Source Code:
The Generators should still be very specific, so it is clear the code is always generated at the beginning of the method and in the order of the attributes.
This simular but differnt to #6063 since here the generated code is defined and no methods referenced
Attribute Implementation:
Geneated Code:
Also standard attributes like [NotNull] [NotNullOrEmpty] [NotNullOrWhitespace] [GreaterZero] [NotNegative] [NotDefault] (for guids for example) could be included in the BCL.
Handing Attribute Properties & Shared code
Lets say we want attributes that take parameters and call our own method
The referenced method could then be generated conditionally in a normal source generator or defined in a referenced assembly.
Generated Code
Beta Was this translation helpful? Give feedback.
All reactions