Use of Singletons

Hi, Group:

I posted an issue a few weeks back regarding a problem where, intermittently, dependencies were not getting injected (or appeared not to be). System would run fine, for some time, then suddenly, calls to dependencies would begin failing saying the object did not exist.

Several people responded with statements to the effect that I was injecting (through property injection), objects that were not singletons and that could be causing the problem.

That creates, for me, the question as to “what” so be defined as a singleton so as to avoid the issue. Is there a “rule of thumb” on this? We are refactoring all our models and services to remove the singleton attribute from the component definition.

Any responses would be appreciated.

Kevin S. Anderson
Software Engineer

Singleton: Instantiated once for the life of the application. Contains ZERO data specific to a single user (i.e. it’s for general purpose functionality, not for a particular user).

That’s the rule.

Many things fall into the category of being a singleton. Anything that is specific to a user - or a request (which is inherently specific to a single user), however, is not a singleton, and if used as a singleton will cause untold problems.

HTH