Instances of Singleton Asp.Net Core classes

Asked

Viewed 317 times

1

I have a doubt in the following situation:

Singleton Classe

namespace TesteSingleton
{
    public class Singleton
    {
        private Singleton _singleton;

        public Singleton()
        {
            if (_singleton == null)
                _singleton = new Singleton();
        }

        public int CodigoSingleton
        {
            get
            {
                return _singleton.CodigoSingleton;
            }
            set
            {
                _singleton.CodigoSingleton = value;
            }
        }
    }
}

When this instance is created by a request in Asp.net core, and at the same time I make another request that also uses this class, the data of each request (which will be different) is maintained data integrity? The class instance is created for face request performed, even if 2 request is made at the same time?

namespace Singleton
{
    public class SingletonController : ControllerBase
    {

        public IActionResult Index()
        {
            var singleton = new Singleton();
            singleton.CodigoSingleton = 10;

            return OK();
        }
    }
}
  • How do you instantiate it in your code?

  • I edited Virgilio, but I’m going to make her normal;

  • Your class is not in the Singleton standard or Dependency Injection is not being used for this, so each instance is different from each other. If you want to make Singleton need to use another way! That’s what you want to do?

  • In fact it would end up not being a Singleton class, but rather ensure that it is instantiated only 1 time in each request made by 1 user. Almost like a Septin, you know? This class in real scenario, I would like to save some mobile data that I send together in the request, and I mean to maintain the integrity of the data even when there are simultaneous requests. Note: I would not like to mess with Séssion, think that the way I’m doing it would solve my situation?

1 answer

1


Basically this is a standard class Singleton:

public class Singleton
{
    protected Singleton() { } // não tem instanciação pelo construtor

    private static Singleton _singleton;

    public static Singleton Instance 
    {
        get 
        { 
            if (_singleton == null)
            {
                _singleton = new Singleton();
            }
            return _singleton;
        }
    }       

    public int CodigoSingleton { get; set; }
}

How to use:

Singleton singleton0 = Singleton.Instance;
Singleton singleton1 = Singleton.Instance; // mesma instância de singleton0

Online Example

using System;

class Singleton
{
    protected Singleton() { }
    private static Singleton _singleton;

    public static Singleton Instance 
    {
        get { 
            if (_singleton == null)
            {
                _singleton = new Singleton();
            }
            return _singleton;
        }
    }       

    public int CodigoSingleton { get; set; }
}

public class Program
{
    public static void Main()
    {
        Singleton singleton0 = Singleton.Instance;
        Singleton singleton1 = Singleton.Instance; // mesma instância de singleton0

        singleton0.CodigoSingleton = 5;
        System.Console.WriteLine(singleton1.CodigoSingleton);       
    }
}

Exit

5
  • But the question is, in simultaneous requests between different users, can I guarantee the integrity of each request? That is, that in 1 request is the value 1, and in 2 the value is 2, as created the new instance of the Singleton class as per each request ?

  • @Nicolabogar if you set the dependency injection yes you get this effect, but, your question does not talk about it, it speaks of classes Singleton ... really very confusing ... What do you intend to do? Just remembering that what I did will depend on the application lifecycle

  • I will summarize what I intend to do, for example, for each request I want to save the id of the user who is making the request, and this I will use in several assemblies, in which case I would not want to be using Sesssion, and I was wondering if I can get the data integrity for each Singleton class created by request, I’d like to know how C# manages it.

  • @Nicolabogar you need to use Session, Cookie, or Identity, IE, you are going the wrong way. Use Identity which in addition to logging in and creating a session provides you recovery. To maintain a full identity has to be so.

Browser other questions tagged

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