How to implement dictionaries in the Multithreaded Singleton standard

Asked

Viewed 40 times

2

The pattern Multithread Singleton serves me in order to enable the use of "global variables" in my application. Its use occurs through the resources of get and set, as it is possible to notice.

How similarly, I can do the same with a variable of type Dictionary?

using System;

namespace Program
{
    class Global
    {
        public sealed class Session
        {
            private static volatile Session instance;            
            private static object sync = new Object();

            private Session() { }

            public static Session Instance
            {
                get
                {
                    if (instance == null)
                    {
                        lock (sync)
                        {
                            if (instance == null)
                            {
                                instance = new Session();
                            }
                        }
                    }
                    return instance;
                }

            }            
            public bool glb_login   { get; set; }
            public string UserID    { get; set; }
            public string IPAddress { get; set; }
            public string PortName  { get; set; }    
        }
    }
}
  • 1

    as far as I know a Dictionary is not a serializable element, you would have to implement your own serialization for it. If you want to serialize, it would be easier to use a simpler structure, like IList for example

  • 1

    Caio, I don’t know if it helps you, but there is an example in 2 parts of C# Singleton in Vbmania, namely: DESIGN PATTERN - SINGLETON PART #1 - http://www.vbmania.com.br/index.php?modulo=alhe&id=8830

  • @Fabioin. I thank you, but I can not access the content for not having registration on the site.

  • @Caiodepaulasilva, registration is 100% free...

1 answer

1

The Multithread Singleton standard meets me to enable the use of global variables in my application

According to the code you posted is hard to notice if you really understand the statement you gave.

This standard gives absolute guarantees that the variable is instantiated only once, but does not give any other guarantee. That is, it is still possible for two threads to write to a property of the existing object at the same time, causing state problems.

If the goal is to ensure that two threads cannot write at the same time, then the code is not correct. A possible solution would be the following:

class Global
{
    public class SessionState
    {
        public bool glb_login   { get; set; }
        public string UserID    { get; set; }
        public string IPAddress { get; set; }
        public string PortName  { get; set; } 
        //Outras propriedades

        public SessionState Clone(){
            //Atenção este método copia as propriedades por valor!
            //Isto quer dizer que se você tiver uma referência, o objeto alterado será o mesmo
            return MemberwiseClone() as SessionState; 
        }
    }

    public sealed class Session
    {
        private static volatile Session instance;            
        private static object sync = new Object();
        private SessionState _sessionState;
        private Session() { }

        public static Session Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (sync)
                    {
                        if (instance == null)
                        {
                            instance = new Session();
                            _sessionState = new SessionState();
                        }
                    }
                }
                return instance;
            }

        }

        public SessionState State {
            get{
                lock (sync)
                {
                    return _sessionState.Clone();
                }
            }
            set{
                lock (sync)
                {
                    _sessionState = value;
                }
            }
        }
    }
}

Now adding your dictionary would be a matter of changing the method Clone to return a new instance with the new dictionary, with the same data. And accessing your session would be done through Session.Instance.State.

Out of curiosity this whole confusion of this pattern can also be replaced by a much simpler version:

private static Session instance = new Session(){
    State = new SessionState()
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.