Skip to content

Conversation

@angularsen
Copy link
Owner

@angularsen angularsen commented Jun 18, 2023

Added UnitsNetSetup to gather global state in a single place as a singleton, with the possibility of passing instances of it to static methods later to override the defaults.

This way, state is no longer scattered among multiple classes that depend on each other in non-obvious ways and where initialization order has caused issues up until now.

Changes

  • Add UnitsNetSetup to hold global state as a singleton property Default that controls default state initialization.
    • Add properties for all "services" that depend on global state: UnitConverter, UnitAbbreviationsCache, UnitParser, QuantityParser
  • Forward all other Default singleton properties from these services to UnitsNetSetup.Default and mark obsolete
  • Add some TODOs where it seems functionality is missing

Testing

❌ Still running into a handful of flaky tests due to racing conditions.
This was a regression in #1210, but still not fixed.
Will investigate and fix in separate PR.

UnitAbbreviationsCacheTests.AllUnitsImplementToStringForInvariantCulture
UnitAbbreviationsCacheTests.MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits

@angularsen angularsen changed the title Add UnitsNetSetup to hold global static state ✨Add UnitsNetSetup to hold global static state Jun 18, 2023
@angularsen angularsen marked this pull request as ready for review June 18, 2023 22:18
@angularsen angularsen enabled auto-merge (squash) June 18, 2023 22:19
Fix static init racing condition by replacing {get;} with =>
Move unit abbreviations out of UnitInfo and into UnitAbbreviationsCache, so manipulating one cache instance won't affect another instance.
@angularsen angularsen merged commit 5f13d99 into master Jul 11, 2023
@angularsen angularsen deleted the agl/unitsnetsetup branch July 11, 2023 00:29
@angularsen
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants