Could you count this singleton / singleton?

Imagine that in the Global.asax.cs file I had an instance class as a private field. So to speak:

private MyClass _myClass = new MyClass();

And I had a static method for Global called GetMyClass () that gets the current HttpApplication and returns this instance.

public static MyClass GetMyClass()
{
    return ((Global)HttpContext.Current.ApplicationInstance)._myClass;
}

So, I can get an instance for current httpapplication requests by calling Global.GetMyClass ().

Keep in mind that there is more than one (global) HttpApplication. For each request there is HttpApplication, and they are merged / separated, so in the literal sense it is not a real singleton . But it really matches the pattern.

So, as the question asked, do you consider this at least a singleton pattern?

, ? ? , , .

, ?

, , , . - , ? , / /?

- - ?

, , , . , .

, .

, . , , . . , . , .

+3
6

, cookie- Singleton, - , Singleton:

  • , / .
  • , .
+4

.NET, , :

, .

- " ". , . - ( , , ), - " " "".

" ", " ": .

" ": singleton

, , , - , , . , , , .

+4

- , CAN .

, , .

+3

-, , - , , , . , IIS ( "Web-Garden", # machine.config). , , , , .

HttpCache. , , - , - , ( , / ). , , :

public SomeClassType SomeProperty
{
    get
    {
        if (HttpContext.Current.Cache["SomeKey"] == null)
        {
            HttpContext.Current.Cache.Add("SomeKey", new SomeClass(), null,
                              System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1),
                              CacheItemPriority.NotRemovable, null);
        }
        return (SomeClassType) HttpContext.Current.Cache["SomeKey"];
    }
}

, , - (), , .

+2

.

, . .

... . -, .

+1

, . . , , .

, HttpApplications, Singleton .

, singleton . , , .

0
source

All Articles