locked
Singleton and Interface RRS feed

  • Question

  • User-1153490049 posted

    I want to implement the interface singleton, same like generic singleton.

     

    public class Singleton<T>

     {

    }

    Singleton<OnlineUser>..

     

    So how to implement Singleton<IOnlineUser> ?

     

     

    Monday, August 30, 2010 3:23 AM

Answers

  • User-1179452826 posted

    It's not exactly easy to implement a generic singleton. Just read this:

    http://www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx

    What I would recommend is that you use an IOC container like Unity, Autofac, StructureMap etc. and register an object using a Singleton lifetime manager.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 30, 2010 6:10 AM
  • User-1179452826 posted

    Raghav, the question is about implementing a generic singleton, and while your implementation is spot on, it's far from generic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 30, 2010 6:21 AM
  • User-1179452826 posted

    Please see my first post. It's not really a simple or pragmatic solution to even attempt. The whole purpose of generics is to make working with different types in a strongly typed manner simple and elegant. That's something that's really not possible. I would use an IOC container for this scenario 10 times out of 10. With unity, this would look something like this:

    var container = new UnityContainer();
    var instance = new OnlineUser(); //OnlineUser implements IOnlineUser
    container.RegisterInstance<IOnlineUser>(instance);
    ...

    ...

    var fetchSingletonInstance = container.Resolve<IOnlineUser>(); 


    You'd put the container in Application or some other form of global scope.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 31, 2010 1:31 AM

All replies

  • User-1179452826 posted

    It's not exactly easy to implement a generic singleton. Just read this:

    http://www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx

    What I would recommend is that you use an IOC container like Unity, Autofac, StructureMap etc. and register an object using a Singleton lifetime manager.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 30, 2010 6:10 AM
  • User1281381861 posted

    Actually the main stuff while going for singleton will be that you are guaranteed that you will get unique instance. You go with lock . Below is the sample code:


    public class Singleton
    {
        private volatile static Singleton uniqueInstance;
        private static readonly object padlock = new object();
        private Singleton() { }
        public static Singleton getInstance()
        {
            if (uniqueInstance == null)
            {
                lock (padlock)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new Singleton();
                    }
                }
            }
            return uniqueInstance;
        }
    }
    In the above code double checking is implemented ,first it is checked if an instance is is created and if not lock has been established .Once in this block
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new Singleton();
                    }
    if the instance is null then create it.
    Also, the uniqueInstance variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed.



    public class Singleton
    
    {
    
    
    
        private volatile static Singleton uniqueInstance;
    
        private static readonly object padlock = new object();
    
    
    
        private Singleton() { }
    
    
    
        public static Singleton getInstance()
    
        {
    
            if (uniqueInstance == null)
    
            {
    
                lock (padlock)
    
                {
    
                    if (uniqueInstance == null)
    
                    {
    
                        uniqueInstance = new Singleton();
    
                    }
    
                }
    
            }
    
            return uniqueInstance;
    
        }
    
    }




    In the above code double checking is implemented ,first it is checked if an instance is is created and if not lock has been established . Once in this block


                    if (uniqueInstance == null)
    
                    {
    
                        uniqueInstance = new Singleton();
    
                    }




    if the instance is null then create it.


    Also, the uniqueInstance variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed.



    Monday, August 30, 2010 6:13 AM
  • User-1179452826 posted

    Raghav, the question is about implementing a generic singleton, and while your implementation is spot on, it's far from generic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 30, 2010 6:21 AM
  • User-1153490049 posted

    So how to implement

                Singleton<IOnlineUser>.Instance ? 

    yes, use interface.

    Tuesday, August 31, 2010 1:17 AM
  • User-1179452826 posted

    Please see my first post. It's not really a simple or pragmatic solution to even attempt. The whole purpose of generics is to make working with different types in a strongly typed manner simple and elegant. That's something that's really not possible. I would use an IOC container for this scenario 10 times out of 10. With unity, this would look something like this:

    var container = new UnityContainer();
    var instance = new OnlineUser(); //OnlineUser implements IOnlineUser
    container.RegisterInstance<IOnlineUser>(instance);
    ...

    ...

    var fetchSingletonInstance = container.Resolve<IOnlineUser>(); 


    You'd put the container in Application or some other form of global scope.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 31, 2010 1:31 AM