Core: ServiceRegistry, GetOrCreate<T> and Remove(instance) #2599
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Details
Adds two new methods to the service registry to help manage singleton-like objects and improve safety when un-registering services.
This is the shortest form to get and create a service if it doesn't exist:
Now this is fine, but singletons are more often than not used all over the place throughout a codebase, meaning that users will either have to copy paste the above, or create an extension method.
This pr would reduce the above into the following:
It is also easier to maintain as users now have only one entry point to their singleton's construction mechanism, see this implementation for the singleton above:
There are a couple of cases like this one in the engine's source.
The remove is there to improve safety when un-registering them, the following usage would be problematic without the instance safeguard.
And they stay threadsafe, like the rest of this type.
Related Issue
None.
Types of changes
Checklist