-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
The Singleton<T> class should be removed #3134
Comments
Hello Sergio0694, thank you for opening an issue with us! I have automatically added a "needs triage" label to help get things started. Our team will investigate the issue and help solve it ASAP. Other community members may also look into the issue and provide feedback 🙌 |
@Sergio0694 I think it would be better to add a |
@yanxiaodi Thanks! 😄 But yeah in general I completely agree, in most situations one should also add a private constructor to make sure that developers don't use the class improperly 👍 |
This was fixed in #3435, closing this 🚀 |
@Sergio0694 did we have a doc on this feature already we need to update to just provide the proper guidance? |
@michael-hawker We have this docs page that was already marked as obsolete with a link to this issue since 6.1 was released: https://docs.microsoft.com/en-us/dotnet/api/microsoft.toolkit.helpers.singleton-1?view=win-comm-toolkit-dotnet-stable/. Do you want to add more details to it with another docs PR? 🙂 |
@Sergio0694 yeah, since that API doc will disappear, it's probably good for us to have a docs PR since this is a common thing that comes up. Just something we can but out there in the wild. |
@Sergio0694 think there's a place in the .NET docs we can better highlight folks searching how to perform this pattern? in reference to: #4108 |
This issue relates to the
Singleton<T>
type defined inMicrosoft.Toolkit.Helpers.Singleton.cs
.I'm wondering whether having this type in the toolkit at all is a good idea, considering it can be fully replaced by only using built-in C# language features, which also result in a faster execution.
The current implementation of the
Singleton<T>
class is as follows:This works, but there are a few issues with this implementation:
_instances
field is within a generic type! This means that for eachSingleton<T>
type, that field will be different, and an entirely newConcurrentDictionary<Type, T>
instance will be built. Furthermore, since eachT
instance is only created once, it means that that dictionary will never really be used at all. When theInstance
property is accessed,_instances
will always either be empty, or with a single object of typeT
, since that's the one being looked for in the currentSingleton<T>
type. Might as well just use a statically initialized field._instance
being statically initialized, since the static class constructor is run under a lock (see here and here). But this means that the same thread-safe behavior could be achieved without the need of aSingleton<T>
class in the first place.Consider this snippet:
This is translated to (see here):
Which again, causes the C# compiler to emit this code in the static constructor for
MyClass
:And this static constructor (see linked issues above) is run under a lock in a given app-domain and only executed once, This is all that's necessary to ensure that a given
T
instance is only created once when the staticT Instance
property is accessed the first time (since doing so triggers the static constructor).Suggestions:
Singleton<T>
class entirely from the toolkit.T Instance
field for the singleton pattern, without the need of additional APIs at all.The text was updated successfully, but these errors were encountered: