[Feature Request] Language support for singletons via simple context keyword #1570
Replies: 11 comments
-
I don't think singleton is a pattern that should be promoted by the compiler. And you're not saving that much code. Also, a similar feature has been suggested before: #490. |
Beta Was this translation helpful? Give feedback.
-
Also, your singleton design isn't thread-safe, which may or may not be required for certain scenarios. |
Beta Was this translation helpful? Give feedback.
-
I wouldn't want C# encouraging the use of singletons. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
We already have singletons. The keyword to make one is |
Beta Was this translation helpful? Give feedback.
-
More seriously, I would rather discuss allowing a That would handle most uniquely singleton scenarios I can think of. |
Beta Was this translation helpful? Give feedback.
-
Singleton was very popular in the days of Java pattern explosion, as a way to bring old global stateful functions to be OO (in style) thus "better", as well as solving the problem of having to pass on many objects everywhere in a "pure" OO program. It was promoted as a super cool pattern. But in reality, there are very few things which are really singleton. Not a manager, not a factory, even not a CPU, not a keyboard... When multi-thread became a thing, the singletons were suddenly evil. They have to be thread-safe, thus full of locks, which then causes hungry cores and deadlocks. When TDD came, they became more evil because they can hardly be mocked. Typically, what you need is an IoC container, then the singleton-ness is managed by that in a scope: per container or per request, etc., instead of global-only. If the class is stateless, I haven't written a singleton for almost 10 years. |
Beta Was this translation helpful? Give feedback.
-
Just few points to think about.
The singleton pattern isn't evil but it became evil or an anti-pattern because people abused it so it really has nothing to do with threading at all but how it was applied and people are still abusing it to this day by omitting the word pattern/implementation from it as if it exists in a vacuum.
No one really need a DI container, sometimes custom made factories is all you need, how smart the factory is should be an implementation detail. However, it's true that objects life-cycle should be handled at the factory level, whatever it is because that's where objects should be created.
Static class can't implement an interface. Static class isn't inheritable. Static class can't be loaded lazily, it's up to the run-time. Not all singleton implementations use static under the hood. Not all singleton implementations have state and there's still a value to that. |
Beta Was this translation helpful? Give feedback.
-
Singleton instances are extremely useful - many of the projects I've worked on have made use of them. That said, none of those projects enforced singleton through code tricks - it was handled as a runtime policy. Also see my earlier comments on #490 |
Beta Was this translation helpful? Give feedback.
-
Obligatory reference to #107. |
Beta Was this translation helpful? Give feedback.
-
This should now be easy to generate via C# 9 source generators |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Singleton, I find, to be a very common design pattern, and I get tired of having to rewrite the same setup every time I go to use it. Instead of creating singletons like this:
Which is already using a couple of features to make it a lot shorter and quicker, we can just do this:
And C# will do the rest of us for us. The constructor will be automatically made
private
and the lazy-loadedInstance
property created in the background. It could also do stuff like throwing an exception if you try to create an instance when one already exists,This makes it very easy to change if a class should be a singleton if you've realized that it should be without having to do the same work again and again.
Beta Was this translation helpful? Give feedback.
All reactions