简体   繁体   中英

Use static global variable class in ASP.NET MVC web application

I am creating an ASP.NET MVC web application. It has service classes to execute business logic and it access data through Entity Framework.

I want to change some business logic based on application variable. These variables are global variables and load from app config and don't change after the initial loading.

public class BroadcastService : IBroadcastService
{
    private static readonly ILog Logger = LogProvider.GetCurrentLogger();
    private readonly IUnitOfWork _worker;
    private readonly IGlobalService _globalService;

    public BroadcastService(IUnitOfWork worker, IGlobalService globalService)
    {
      _worker = worker;
      _globalService = globalService;
    }

    public IEnumerable<ListItemModel> GetBroadcastGroups()
    {
       if(Global.EnableMultiTenant)
       {
          //load data for all tenants
       }
       else
       {
           //load data for current tenant only
       }

       return broadcastGroups ?? new List<ListItemModel>();
    }

    ...
}

public static class Global
{
    public static bool EnableMultiTenant{get;set;}
}

For example, EnableMultiTenant will hold application is running in multi-tenant mode or not.

My concerns are:

  1. Is it ok to use a static global variable class to holds those values?

  2. This application is hosting on Azure app service with load balancing. Is there any effect when running multi-instance and when app pool restarts?

To answer your question as to whether it is 'okay' to do this, I think that comes down to you.

I think the biggest thing to know is when that data is going to get refreshed. From experience I believe that static information gets stored in the application pool, so if it is restarted then the information will be refreshed.

Lifetime of ASP.NET Static Variable

Consider how many times you need that information, if you only need it once at startup, is it worth having it as a static. If you are getting that information a lot (and say for example it is stored in a database) then it may be sensible to store that in a cache somewhere such as a static member.

I think my only recommendation with static member variables is asp is keep them simple, booleans seem fine to me. Remember that users do share the same application meaning that static variables are global for all users. If you want a user specific variable then you want to use sessions cache.

Always remember the two hardest thing in programming

  1. Naming things
  2. Cache invalidation
  3. Off by one errors

https://martinfowler.com/bliki/TwoHardThings.html

Even though this is a joke, it holds a lot of truth

Hope this helps

This is thread safe if you initialize these values once and then only read from them. It is also safe in the presence of multiple worker processes and restarts because the multiple processes don't share variables.

As an alternative consider creating an instance of a class holding your settings:

class MySettings {
 bool IsEnabled;
}

Then you can use dependency injection to inject a singleton value of this class to your code. This makes it easier to tests and makes the code more uniform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM