简体   繁体   English

Asp.Net MVC3 Singleton变量

[英]Asp.Net MVC3 Singleton variable

It is chance define some singleton variable in asp.net mvc3 ? 有机会在asp.net mvc3中定义一些单例变量吗? I want define it (probably in global.asax) and then in controllers get it, by IOC resolver (structureMap) 我想定义它(可能在global.asax中),然后在控制器中通过IOC解析器(structureMap)来获取它

Simple: I want have one property with same data for all users, not dependency on request, session, ... For example At first start of MVC application I set DateTime value into this property and then all requests can view this property and modified this property in controllers. 简单:我想为所有用户提供一个具有相同数据的属性,而不是依赖于请求,会话等。例如,在MVC应用程序的第一次启动时,我将DateTime值设置为该属性,然后所有请求都可以查看该属性并对其进行修改控制器中的属性。

Just use StructureMap to create your controllers using a dependency resolver like outlined in this blog post . 只需使用StructureMap使用依赖解决程序来创建您的控制器,如本博文中所述。 Inject not the variable directly but an instance that holds that variable: 不直接注入变量,而是注入包含该变量的实例:

public class GlobalScope 
{
  public String Variable { get; set; }
}

Now you can inject GlobalScope into your controllers and set/get the Variable but keep in mind that you're in a multi threaded environment where read and write access to variables must be synchronized or you run into some weird race conditions. 现在,您可以将GlobalScope注入控制器中并设置/获取变量,但请记住,您处于多线程环境中,必须同步对变量的读写访问,否则您将遇到一些奇怪的竞争条件。

First of all you can use simple singleton pattern in .net wihich is not related to asp.net or asp.net mvc specific things at all. 首先,您可以在.net中使用简单的单例模式,而这与asp.net或asp.net mvc完全无关。

If you want to use IoC, you can register you class instance like single instance and IoC will return you always only single class instance. 如果您想使用IoC,则可以像单实例一样注册类实例,而IoC总是只返回单个类实例。 Put necessary properties to this instance and edit it. 将必要的属性放入此实例并进行编辑。 Initialization will be on the type registration step: 初始化将在类型注册步骤中进行:

Sample in Autofac : Autofac中的示例

builder.Register(MySingleton.Instance);

and with Lazy initialization: 并使用惰性初始化:

builder.Register(c => MySingleton.Instance);

Also you can register type and IoC will control your type initialization, but in this case you can't set your default value during initialization: 您还可以注册类型,并且IoC将控制您的类型初始化,但是在这种情况下,您无法在初始化期间设置默认值:

builder.RegisterType<MyType>().SingleInstance();

Any Variable or class that is public Static resides in the AppDomain and is globally availiable for use in all sessions. 公共static的任何变量或类都驻留在AppDomain中,并且在所有会话中都可以全局使用。 So you could simply: 因此,您可以简单地:

  • Create a Public Static Class Called "CurrentApplication" in APP_CODE and then use it in all sessions. 在APP_CODE中创建一个称为“ CurrentApplication”的公共静态类,然后在所有会话中使用它。
  • Also IOC works great for this as well because most IOC object default Lifestyle is Singleton and therefore resides in the AppDomain as well. 由于大多数IOC对象默认的Lifestyle是Singleton,因此IOC对此也非常有用,因此也位于AppDomain中。
  • Another way could be to use a "Common Service locator" 另一种方法是使用“公共服务定位器”

Have been known to use all 3 including using a Common Service Location to find the IOC container, which makes life so easy. 众所周知,可以使用这3种方法,包括使用通用服务位置来查找IOC容器,这使生活变得如此轻松。

Hope this Helps 希望这可以帮助

You could store the data as a static member of the Application class. 您可以将数据存储为Application类的静态成员。 See this article - Application State in ASP.NET 请参阅本文-ASP.NET中的应用程序状态

Your IoC container probably extends the concept of a singleton. 您的IoC容器可能扩展了单例的概念。 Ninject does with InSingletonScope. Ninject使用InSingletonScope。 This would work better than a static app variable as any libraries would have to know about your application, and then you have circular references. 这将比静态应用程序变量更好,因为任何库都必须了解您的应用程序,然后才有了循环引用。

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

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