-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes ReactiveValidationObject NullReferenceException and enhances performance and thread-safety of ValidationText #144
Conversation
…ion to immutability.
… to provide a `ValidationText.Empty` property).
…ate, so that `Empty` contains single empty string. Updated use of obsolete constructors.
Passed tests.
Codecov Report
@@ Coverage Diff @@
## main #144 +/- ##
==========================================
+ Coverage 61.57% 61.94% +0.36%
==========================================
Files 16 16
Lines 877 896 +19
==========================================
+ Hits 540 555 +15
- Misses 337 341 +4
Continue to review full report at Codecov.
|
Restored UAP target.
@worldbeater does this need tests adding for the Create factory? Not sure where the best place to add such tests would be, also I'm a bit scuppered by #145 at the moment (though I can temporarily delete the UAP target to test). |
Worth adding a few unit tests for the factory methods yeah as we are exposing them as public API @thargy. This should make Removing the UAP target and not including this particular .csproj change in the diff (e.g. via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be happy to see the unit tests for the factories but in general LGTM! Thanks for this.
Updating this now. |
… no items - this is used when initialising a ValidationContext. ValidationText.Empty always contains a single item which is string.Empty. Previous usages of the constructors have been changed to make use of the Create methods, and the Create methods correctly re-use the singletons where possible.
When Add() & Clear() are obsolesced this should be made read-only.ReactiveUI.Validation/src/ReactiveUI.Validation/Collections/ValidationText.cs Lines 29 to 34 in 0111a81
This comment was generated by todo based on a
|
I believe this is now done! After careful review, I added both an For example, The PR should improve thread safety and memory utilisation quite noticeably as the None and Empty singletons are a very common use case in the codebase (and elsewhere). This will result in a lot fewer object allocations in real-world scenarios. |
Sorry for those boring suggestions! Just making sure we didn't miss anything. Huge thanks for the PR! |
Co-authored-by: Artyom V. Gorchakov <[email protected]>
When Add() & Clear() are obsolesced this should be made read-only.ReactiveUI.Validation/src/ReactiveUI.Validation/Collections/ValidationText.cs Lines 29 to 34 in 17f0792
This comment was generated by todo based on a
|
Co-authored-by: Artyom V. Gorchakov <[email protected]>
When Add() & Clear() are obsolesced this should be made read-only.ReactiveUI.Validation/src/ReactiveUI.Validation/Collections/ValidationText.cs Lines 29 to 34 in 2c54af4
This comment was generated by todo based on a
|
Lets see if we can remove some of these TODOs and put them into a github issue. |
Thanks @worldbeater & @glennawatson for taking it over the line, I'd gone to bed! |
Should be available on NuGet as |
What kind of change does this PR introduce?
This pull request fixes #142 and implements #141. There is no good reason for
ValidationText
to be mutable, as the current mutators (Add
andClear
) are not used by the library and introduce potential thread-safety issues when validating asynchronously.What is the current behavior?
#142 describes a bug based on the nullability of
ReactiveValidationObject.SelectInvalidPropertyValidations
, the fix is included in this PR as it is dependent on changes implemented for #141#141 describes the benefits of making
ValidationText
immutable. As well as thread-safety, we can provide theValidationText.Empty
andValidationText.None
singletons for improved performance and memory utilisation. The current behaviour creates a lot of unnecessary lists.What is the new behavior?
This PR introduces a pseudo-immutable version of
ValidationText
which exposes newCreate
factory methods. The factory methods will automatically detect aNone
orEmpty
equivalent and return the new corresponding singleton instead of a new instance, reducing memory utilisation.The underlying
List<string>
is replaced with astring[]
to improve performance and memory utilisation.Currently, the underlying array is not
readonly
as theAdd()
andClear()
mutators (now marked as obsolete) are implemented for backwards compatibility. Using those methods will break thread-safety (which is not a breaking change). If those methods are not used, the class is thread-safe.The existing constructors are marked as obsolete to encourage adoption of the factory methods.
TODO comments are included for changes that can be made when the existing constructors/methods are finally removed in future.
What might this PR break?
The provided implementation should be backwards compatible, it marks certain methods/constructors obsolete which will produce warnings in consumers.
Please check if the PR fulfills these requirements
Other information:
Existing usages of the now obsolete constructors have been removed in the library and replaced with calls to the factory methods. As such these frequently return one of the new singletons. Trying to call
Add()
orClear()
on the singletons results in anInvalidOperationException
to prevent more serious bugs.Where consumers created their own
ValidationText
instances this is not a problem, as the singletons will not be in use in existing code. However, attempting to modifyValidationText
instances provided by the library itself may result in the new exception. This is considered unlikely in practice.