C# Dirty/Versioned Records #4271
-
Now that C# 9.0 has support for records, I believe including "dirty" and version/revision support would be greatly beneficial. Whenever records are constructed, either by calling the constructor or using object initialization, an implicit "dirty" flag should be set to false. Any instance created through the use of the "with" operator, should have its "dirty" flag set to true. A natural extension to this is to have an implicit version/revision number. Whenever records are constructed, either by calling the constructor or using object initialization, an implicit "version/revision" field/property should be set to 1/0. Any instance created through the use of the "with" operator, should have its "version/revision" field/property incremented. Consider:
Of course, this is a trivial example that is just meant to show the concept. The real benefit is when used in a more elaborate scenario or in async contexts (e.g. Rosyln or Entity Framework). |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 27 replies
-
I believe the real problem with this proposal comes with the detail - there are too many edge cases where different teams/projects/developers will want different behaviour. For just one example, if I reset the value of a property back to the original value, should the instance be considered dirty? I can make a disciplined logical argument that it should be considered dirty. |
Beta Was this translation helpful? Give feedback.
-
What's the advantage of the proposed code over just: person = CheckCapitalization(person); Or maybe (assuming the assignment is not trivial): var newPerson = CheckCapitalization(person);
if (!ReferenceEquals(newPerson, person))
person = newPerson; Also, why should every single record pay the performance penalty for this, when it's likely that only a small minority of them will use this feature? |
Beta Was this translation helpful? Give feedback.
-
This solution obviously isn't a silver bullet. With this language support, however, following a simple guideline of each updater only performing a single record.with update makes writing this kind of code simple and easy to reason about. Try implementing this logic without this feature and you will see what I mean. One thing to keep in mind is that all the language would be doing is incrementing version numbers. How you choose to use the version is up to you. |
Beta Was this translation helpful? Give feedback.
-
This seems like a feature better offered by a source generator, one that can be specifically provided by Entity Framework that will work well with the policies that make sense for Entity Framework. [Versioned]
partial record Person(string firstName, string lastName); |
Beta Was this translation helpful? Give feedback.
-
First, let me say that i find the idea of having some sort of versioning concept very interesting. It's certainly something that arises in some domains (including ones that Roslyn plays in). However, i also think this is something extremely domain specific and fiddly. Different domains do this very differently and tehre are often many knobs you need to deal with something like this. Because of that, i don't feel like this should be something baked into either hte language or runtime. Instead, it would make a ton more sense for this to be brought in by a source generator that can be customized to the level that people will invariably need. Because practically all parts of a record can be implemented manually, it would be totally fine to let people declare a So i strongly approve someone pursuing this space. However, i think the right place would not be in the language or runtime. Cheers! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback guys. I guess we can close this out. I'll go with @HaloFour's suggestion. Closing thought. Regarding the major/minor approach, I just realized that the "increment" on assignment is unnecessary. As long as the "version" keyword does the "roll up", everything works. Thanks again and happy holidays. |
Beta Was this translation helpful? Give feedback.
This seems like a feature better offered by a source generator, one that can be specifically provided by Entity Framework that will work well with the policies that make sense for Entity Framework.