0

I have an application and my desing approach look like this:

class Manager
{
    public int State;

    static Manager _instance = null;
    public static Manager Instance
    {
        get { return _instance; }
        set
        {
            if (_instance == value)
                return;
            _instance = value;
        }
    }

    public Manager()
    {
        State = 0;
        Instance=this;
    }
}
class Module1
{
    public void GetState()
    {
        Console.WriteLine(Manager.Instance.State);
    }
}
class Module2
{
    public void GetState()
    {
        Console.WriteLine(Manager.Instance.State);
    }
}
class Module3
{
    public void GetState()
    {
        Console.WriteLine(Manager.Instance.State);
    }
}

Manager class already registered in Bootstrapper like :

 protected override void ConfigureContainer()
 {
       base.ConfigureContainer();
       Container.RegisterType<Manager>(new ContainerControlledLifetimeManager());
 }
 protected override void InitializeModules()
 {
      Manager man= Container.Resolve<Manager>();
 }

Question is do I need to define my manager object as static in its field to be able to reach its state? Or this is anti pattern or bad for performance?

  • Static (global) access is almost always a bad thing. You can get an instance from the container and pass it into things, which is safer. I won't go into detail on why static access to state is bad. That has been done by many over the years far more clearly than I am most likely capable of. – Magus Jun 6 '14 at 15:19
  • Static fields that are exposed via properties or methods are functionally the same as global variables. You can search on that to see why they are bad. – Frank Hileman Jun 7 '14 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.