ASP.Net Architecture Specific to Shared/Static fun

2019-06-08 11:53发布

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?

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?

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-08 12:40

everything is visible all the time! classes, static functions, functions on classes, etc. the only thing that is not constant is the data loaded.

Static data remains across requests (maybe, applications can be flushed). Generally, per request, you load the objects / data you want, and present it in whatever way you want.

Static functions that work on static data need to be aware of threading issues.

查看更多
男人必须洒脱
3楼-- · 2019-06-08 12:52

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.

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;
                    }
                }

        }

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();

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.

查看更多
登录 后发表回答