简体   繁体   English

在ASP.NET MVC Web应用程序中使用静态全局变量类

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

I am creating an ASP.NET MVC web application. 我正在创建一个ASP.NET MVC Web应用程序。 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. 例如, EnableMultiTenant将使应用程序是否以多租户模式运行。

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. 此应用程序通过负载平衡托管在Azure应用程序服务上。 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 ASP.NET静态变量的生命周期

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. 我认为我对静态成员变量的唯一建议是asp是让它们保持简单,布尔值对我来说似乎很好。 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 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. 这使得测试更加容易,并使代码更加统一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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