ASP.Net架构具体到共享/静态函数 [英] ASP.Net Architecture Specific to Shared/Static functions

查看:119
本文介绍了ASP.Net架构具体到共享/静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请告知在ASP.Net应用程序的情况下是共同的所有用户共享/静态函数?

Could someone please advise in the context of a ASP.Net application is a shared/static function common to all users?

例如,如果你有一个函数

If for example you have a function

Public shared function GetStockByID(StockID as Guid) as Stock 

这是常见的功能到应用程序的所有当前用户?或者是共享功能只特定于当前用户和只在该当前用户的上下文共享?

Is that function common to all current users of your application? Or is the shared function only specific to the current user and shared in the context of ONLY that current user?

所以,更确切地说,我的问题是这样的,除了数据库的并发问题,如表锁定我需要线​​程问题关心自己在共享功能于一身的A​​SP.Net应用程序?

So more specifically my question is this, besides database concurrency issues such as table locking do I need to concern myself with threading issues in shared functions in an ASP.Net application?

在我的头上;比方说,我的应用程序命名空间是MyTestApplicationNamespace。每当一个新用户连接到我的网站创建MyTestApplicationNamespace的新实例,因此所有共享功能适用于该实例和用户,但在多个用户并不常见。这是正确的?

In my head; let’s say my application namespace is MyTestApplicationNamespace. Everytime a new user connects to my site a new instance of the MyTestApplicationNamespace is created and therefore all shared functions are common to that instance and user but NOT common across multiple users. Is this correct?

推荐答案

命名空间的任何实例不断创造在运行时。把它看成是组织code,就像在硬盘上的某个目录的方式。

No instance of a namespace is ever "created" at runtime. Think of it as a way of organizing code, like a directory on the hard disk.

我想分享的方法只是一个code的块,也可以不实例化一个对象运行。因此,每个用户将是具有直通运行的code自己的逻辑独立的进程。

I think of shared method as just a chunk of code that can be run without instantiating an object. So every user will be having their own logical independent process running thru the code.

如果您希望通过它可以像下面这样做的每个用户共享一个单独的对象:

If you want a single object that is shared by each user it can be done like the following:

     public class cApp
        {

                static readonly cDB _cDB = 
new cDB(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);


                public static cDB DB
                {
                    get
                    {
                        return _cDB;
                    }
                }

        }

本实例类型CDB的一个对象第一次是在一个线程保存方式感动。它将被称为_cDB可以在code使用直通财产DB。这样的:

This instantiates a single object of type cDB the first time it is touched in a thread save way. It will be called _cDB that can be used in code thru the property DB. Like:

cApp.DB.MyMethod();

这只会有实例化一个实例,但同样每个用户都将独立执行code。所以私有变量的值是由每一个过程(如在实施例的连接串)看到的,但局部的方法设置一个局部变量不会影响通过相同code运行的任何其他过程

This will only have one instance instantiated, but again each user will execute the code independently. So the value of the private variables are seen by each process (like the connection string in the example), but a local method setting a local variable will not affect any other process running through that same code.

这篇关于ASP.Net架构具体到共享/静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆