Proposal/Discussion: Syntactic sugar for INotifyPropertyChanged/Changing #1074
Replies: 7 comments
-
notify keyword (while it may sound nice) but its very domain specific, I don't think it would be added. one might want keyword for dependency properties, other wants another keyword for dealing with network stuff. there you have pile of keywords that only make sense in distinct areas which is not a good thing. Remember that c# is a general-purpose programming language use of attribute seems good enough, and is already achieved unofficially https://stackoverflow.com/a/18002490/4767498 yet another solution |
Beta Was this translation helpful? Give feedback.
-
So my vote is 👍 for attribute usage. but part of your proposal seems to be using notify keyword which I'm really against it, so I have to 👎 |
Beta Was this translation helpful? Give feedback.
-
Yea, it is domain specific, but it doesn't really clash with anything and the domain is pretty huge (unless you are exclusively web dev, but then most of the .net is domain specific hence .net core). In essence it's not that much different than syntactic sugar with auto-properties or expression-bodied members, or even linq, they also generate hidden code, this is just a larger chunk which leans on an interface that .net extensively uses in all rich client solutions. Not all apps are web, you can't fully escape the domain use, at least the solution I propose does not require changes to IL, its just syntactic sugar. I know some people are against syntactic sugar in general, but that sort of thinking is why Java is now dying and people are running away to Kotlin. As for fody, I generally dislike weaving as a concept, especially on IL level, that stuff tends to pile up and then cause undebuggable issues because generated code behaves differently from the code that was typed making debugging pointless. Since .net officially uses |
Beta Was this translation helpful? Give feedback.
-
The code generation proposal solves INotifyPropertyChanged (and many other scenarios). Hopefully it happens someday. |
Beta Was this translation helpful? Give feedback.
-
I feel your pain. Using the
public List<string> Employees
{
get;
set => SetProperty(ref field, value);
} The last 4 lines are always the same you can copy and paste them or use a snippet for them. Plus if you want to change the property name or the property type, for example to
|
Beta Was this translation helpful? Give feedback.
-
UWP/WPF are extremely dependent on good change notification for data binding. For such a major use case, I would be very comfortable with a builtin language feature for this purpose. |
Beta Was this translation helpful? Give feedback.
-
@quinmars, you are right about comment 1, I extended the example with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently the classes participating in WPF/UWP look really cumbersome:
instead I propose several additions which would help WPF/UWP code be cleaner and easier to make:
Add "notify" to class declaration:
public notify class AuthenticationData
This will automatically implement
INotifyPropertyChanged
andINotifyPropertyChanging
interfaces and inline SECTION 1 code into class , setting the stage.add optional
notify
toset
accessor for auto-property:public string Username { get; notify set; }
This will cause the compiler to use
SetProperty
to change the backstore value. If not specified, the property behaves like a regular c# property as it does now.add optional
Action
andAction<T>
delegate support into defaultnotify set
accessorpublic string Username { get; notify set(() => { /* my code */}); }
All this together will shrink the above class to this:
Alternatively add all this through compiler attribute support, but then we'll lose the ability for local
Action
handling and it will also be more ambiguous :Beta Was this translation helpful? Give feedback.
All reactions