1

I seen here that static class & static functions are bad because they take a lot of memory.

I use it for many things like that need interact with the database etc..

This example of part of the Static Localization class, the GetResources

public static class SFLocalization
{

    public static string GetResources(string key)
    {
          string CurrentLanguage = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
          if (MemoryCache.Default["Resources_" + key] == null)
          {
          string x 
          using (Db _db = new Db())
            {
                MemoryCache.Default["Resources_" + key] = _db.Languages.First(l => l.Key == key && l.LanguageCode == CurrentThread).Value;
            }
          }
          return MemoryCache.Default["Resources_" + key];
    }
}

And then In the view, Controllers etc.. I write just this to get the translated value

 @SFLocalization.GetResources("NewsletterBoxTitle")

1.) Is static class in those situations really so bad?

2.) What is the alternative ? maybe the dependency injection (ninject etc..)?? (I seen it in the book Apress - Pro Asp.net Mvc 4

Community
  • 1
  • 1
Daniel Ezra
  • 1,424
  • 3
  • 14
  • 21
  • 1
    I think you forgot to make "here" (in "I seen *here* that static class & static functions are bad...") a link. What exactly are you referring to? – lc. Jul 18 '13 at 08:47
  • 4
    *"I seen here that static class & static functions are bad because they take a lot of memory."* This is not true. – MattDavey Jul 18 '13 at 08:50
  • 2
    Static classes are less good because they cannot be easily mocked. With IoC you can expose a single instance as a singleton, however you still have control of the object. This way you can mock them into classes as they are a dependency. – Myrtle Jul 18 '13 at 08:50
  • 1
    I partially agree with you since `static class members are never GC'd` but I will never agree to `static functions take lot of memory`. Do functions take memory(it does if locals declared only, that too will be GC'd soon after out of scope)? this statement is Wrong! – Sriram Sakthivel Jul 18 '13 at 08:56
  • So, is it good to take the translated text using static class? – Daniel Ezra Jul 18 '13 at 08:59
  • see the answer of "Jason De Oliveira" in this post http://stackoverflow.com/questions/11128750/static-objects-in-asp-net-a-waste-of-memory – Daniel Ezra Jul 18 '13 at 09:32

1 Answers1

6

A static class (or module) is often a replacement for a Domain or Application service. It's often convenient to expose this service as a static class so that it can be accessed from anywhere - this is especially true if it's a cross-cutting concern such as localization.

This approach can get you up and running quickly, but it does present several problems. Being able to access the service anywhere/any time encourages poor coding practices and can easily lead to spaghetti code. Static modules also make the classes that use them very difficult to unit test. These are both problems which will grow exponentially as your project increases in size - so it's usually a good idea to get them dealt with as soon as possible.

As you mentioned in your question, dependency injection is one way to ensure that classes have access to the service, without making it static/global.

MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • see the answer of "Jason De Oliveira" in this post http://stackoverflow.com/questions/11128750/static-objects-in-asp-net-a-waste-of-memory – Daniel Ezra Jul 18 '13 at 09:29
  • 1
    @DanielEzra is your question a duplicate of that one? – MattDavey Jul 18 '13 at 09:33
  • not, but he they said: "Static object are considered application's roots and are not garbage collected." – Daniel Ezra Jul 18 '13 at 09:46
  • 1
    @DanielEzra That's right, static modules are not eligible for garbage collection and are not unloaded until the appdomain shuts down. I don't see how this is relevant though? – MattDavey Jul 18 '13 at 11:03
  • 1
    What I mean to say is - static objects require neither more nor less than object instances. The only difference is how long they reside in memory. – MattDavey Jul 19 '13 at 11:05