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?
– novic
I edited Virgilio, but I’m going to make her normal;
– Nicola Bogar
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?
– novic
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?
– Nicola Bogar