There should be an easy clean way to implement INotifyPropertyChanged on auto properties #8010
-
I tried to submit this from the dev community site but it got pushed to the rosyln project for reasons. This issue has been moved from a ticket on Developer Community. Auto Properties are wonderful compared to how verbose they were before
But if we need to use INotifyPropertyChanged to support an MVVM pattern we are forced back into the bad old days. This is a frequent problem that leads to hundreds of extra lines of ugly code and time lost. This:
Should be able to be written as:
I do not believe hacky 3rd party code generators are a good solution here. They have been around for over a decade and only serve as a grubby band-aid rather than a clean, elegant solution in the language itself. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 37 replies
-
Hi Adam. Could you use markdown blocks to clean up the code samples? Thanks! :-) |
Beta Was this translation helpful? Give feedback.
-
This can easily be solved by SC. You can easily make a SC and annotate your field/property with some attribute say Maybe something like this should be included in the framework. |
Beta Was this translation helpful? Give feedback.
-
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty seems pretty clean/elegant and thats just one example. Prior to Source Generators becoming first class citizens yeah they might have been hacky, but not anymore. |
Beta Was this translation helpful? Give feedback.
-
That answer is kinda disingenuous, you're showing a PRIVATE property and not the rest of the code that I have to add. |
Beta Was this translation helpful? Give feedback.
-
Isn't this one of the main use cases for semi-auto properties (which are - hopefully! - coming in the next language version)? public string Title { get; set => SetProperty(ref field, value); } where |
Beta Was this translation helpful? Give feedback.
-
I used post sharp and it solves this problem for me. |
Beta Was this translation helpful? Give feedback.
We are going to have to agree to disagree here. I think you are reading hostility where none was intended and I'm sorry for that.
If you guys can't see any way that adding a 3rd party lib to a project could have a negative impact on it then we're just to far away to see eye to eye. I also am not disputing that the generator works, I'm saying adding it to the language would be a nicer solution. If you say it isn't worth the effort, that is fine. Let's leave it at that.