2 ms·
> These ways are simpler than the usual gymnastics to implement singletons. Is this a python-centric complaint? I usually write C# and Singletons are just one
by mikece 5y ago
> These ways are simpler than the usual gymnastics to implement singletons.
Is this a python-centric complaint? I usually write C# and Singletons are just one of a few different scope types for defining objects in the IoC container, no easier, harder, rarer or more common than any other class type.
- emodendroket 5y agoI would consider creating a dependency as a singleton in your composition root different from a "true" singleton, because the latter involves baking the assumption that it will be a singleton into all callers rather than injecting it.
- Matthias247 5y agoI guess so. My favorite problem with singletons is testability. How can you properly mock/observe/etc it inside a test case, if lots of test-cases are executed in parallel. With a real global object it can be tricky. But if the object is not global because an IOC container might still create a fresh one per test, it wouldn't be an issue.
- brtkdotse 5y agoCame here to write this. In .NET it’s easier to just follow the normal DI pattern and inject your desired singleton with AddSingleton<>
- moron4hire 5y agoThe Singleton of .NET IoC containers is not the same thing as the Singleton pattern. It's called a Singleton because the DI controller will make sure only one instance is available. But the Singleton pattern has the class itself making sure it can have only one instance. The mixing of concerns is the problem with the Singleton pattern. With a DI controller, you can change the lifetime of the instance without having to change the class definition itself. With the Singleton pattern, changing the instance lifetime requires changing every utilization site.