views:

75

answers:

2

Hello All,

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?

+1  A: 

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.

JBrooks
Thanks very much for the indepth answer...
Maxim Gershkovich
A: 

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.

Keith Nicholas