Creating Singleton object

Topics: Enterprise Library Core, General discussion
Nov 9, 2007 at 1:52 PM
Hi,

Please forgive my ignorance about the whole object builder framework.
I am trying somehow tell the builder system that a particular object should be a singleton.
My understanding is that I should be able to write my class as a normal class (no singleton pattern implementation details) and then, somehow tell the builder framework or the entlib that object of type MyClass should be provided as singletons.
But at this time, I can't seem to be able to put my finger on what needs to be done for that.

Thanks in advance for your help.

Eric.
Nov 9, 2007 at 7:28 PM
Hi Eric,

You can take a look at the implementation for the CacheManager factory and ObjectBuilder's unit tests for information.

This is a two step process that relies on a Locator object. The SingletonStrategy will look for the an existing object and return it if present, short-circuiting the creation process, while the strategy that creates the object will add it to the locator if necessary. Now, EntLib uses its own creation policy that relies on configuration; in the CacheManager's case, its custom factory will register it to the locator. If you're using plain OB, you would configure ISingletonPolicies for your type that would be consumed by the standard CreationStrategy.

Hope this helps,
Fernando