CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

New Singleton Classes

[Edit: most of these classes have been combined into a single class named Singleton, which is now located in the CG.Core library]

 

I added some new singleton classes to my CG.Common library this weekend. I know what you're thinking, the singleton pattern has to be the most overused programming idiom in existance, but the pattern is still useful for managing the lifetime of an object. I have had the Singletonbase class in my library for ever but I've extended it several times this year so I thought I would share my new code here.

The first new class is called CachedSingletonBase and it looks like this:

public abstract class CachedSingletonBase<T> where T : new()

{

 

    public static T Default

    {

        get

        {

            // Get the instance from cache manager and return it.

            return (T)CacheManager.Default.GetData(

                typeof(T).Namespace + typeof(T).Name

                );

        }

    }

 

    static CachedSingletonBase()

    {

        // Create the instance and store it in the cache manager

        CacheManager.Default.Add(

            typeof(T).Namespace + typeof(T).Name,

            new T()

        );

    }

}

 

Notice that CachedSingletonBase stores the instance member using the CacheManager class. CacheManager also resides in my CG.Common library and actually stands as a placeholder for the Microsoft Enterprise Library Cache class.

The next new class is called ThreadLocalSingletonBase and it looks like this:

public abstract class ThreadLocalSingletonBase<T> where T : new()

{

 

    public static T Default

    {

        get

        {

            // Get the thread-local data slot.

            LocalDataStoreSlot tlsSlot = Thread.GetNamedDataSlot(

                typeof(T).Namespace +

                typeof(T).Name

            );

 

            // Return the data instance.

            return (T)Thread.GetData(tlsSlot);

        }

    }

 

    static ThreadLocalSingletonBase()

    {

        // Create a thread-local data slot to hold the instance.

        LocalDataStoreSlot tlsSlot = Thread.AllocateNamedDataSlot(

            typeof(T).Namespace +

            typeof(T).Name

        );

 

        // Create the instance and store it.

        Thread.SetData(tlsSlot, new T());

    }

 

}

 

ThreadLocalSingletonBase stores the instance member into Thread-Local-Storage. This can come in handy in some multi-threaded situations.

 

The final new class is called WebSingletonBase and it looks like this:

 

public abstract class WebSingletonBase<T> where T : new()

{

 

    public static T Default

    {

        get

        {

            // Get the instance from application state and return it.

            return (T)HttpContext.Current.Application[

                typeof(T).Namespace + typeof(T).Name

                ];

        }

    }

 

    static WebSingletonBase()

    {

        // Create the instance and store it in the application state.

        HttpContext.Current.Application[

            typeof(T).Namespace + typeof(T).Name

            ] = new T();

    }

 

}

 

WebSingletonBase stores the instance member into the ASP.NET application state cache. This comes in handy when working in an ASP.NET application.

 

OK that's all the new goodies for today. Once again, the source for all these classes is inside the CG.Common library. Just navigate to the downloads section if you're interested.

 

Oh yea, so how do you use these classes? Great question! Here's a quick example:

 

class MyClass : CG.Common.WebSingletonBase<MyClass>

{

    public void Foo() { }

}

 

class TestMyClass

{

    public TestMyClass()

    {

        MyClass.Default.Foo();

    }

}

 

Just derive your class from one of the singleton base classes and add your methods and such, then use your new singleton class through the static Default property as in the example above. Pretty easy stuff!

 

 

Have fun! :o)

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement