views:

73

answers:

3

I am working with Asp.NET and I have a class named PathFinder which has methods like StyleBarPath(string BrandId,string ProductId) which returns for example a combination of path including brandId and productId and also there are such methods in the same class.

What I am thinking to make them static methods to invoke them easily in every where by saying PathFinder.StylePath("1","2"); to use the returned values inside a <a href=""></a> user control.

But since I am working too much these days, what I know is getting complicated for some reasons. Anyways, here is my question :

Since I am using inline coding on a lot of places like <a href='<%=PathFinder.StylePath("1","2")%>'></a>, I don't want to create a lot of instances by doing this <a href='<%=new PathFinder().StylePath("1","2")%>'></a> by declaring the methods not-static.

But I am afraid of changing the methods returns values because defining the methods static. I mean when a client calls this method, it wouldn't affect the other clients who invoke the same method at the same time?

They would have different call stacks right?

Lets say :

  • client one invokes the method with these parameters -- {brandId:2,productId:3}
  • client tow invokes the method with these parameters -- {brandId:3,productId:4}

This actions happens near the same time when the server processing their requests. What I want to learn is whether the invocations affect each others and change the returning values of each other since they are defined static.

Thanks for reading so far and being a helper :)

I just don't want the clients see path/1/2/ while they are waiting for path/2/3/

Some Notes about the question :

  • Is it the same for static fields?
+6  A: 

You can call a static method safely from multiple threads simultaneously and at separate times given the following:

  • The static method does not access variables outside of itself, or those variables are also thread safe.
  • The static method does not create side effects which are not thread safe.

If your method is what it looks like it is, you're simply taking some inputs adjusting them and returning the result without accessing anything outside of the function. That would be completely safe.

Static fields are not the same as methods at all. It is one copy of a variable. Any changes to that static field will affect everything else that uses it.

John Fisher
One more question : Static variables are shared throughout the App Domain right? So they should be thread safe if I want to work with static variables and fields ? Since every requests are served in one app domain with different threads, they wouldn't be thread safe.
Braveyard
@Braveyard: It sounds like you understood that correctly. Writable static properties and variables are not threadsafe, unless you go to extra work to make them so.
John Fisher
+1  A: 

In C#, static means (in layman's terms) there is one copy. If you provide arguments to the method, and you return a value based on those parameters only, then you will be perfectly safe.

The only time you might run into problems is if your static method uses any static variables to store data between calls, since that could make call-1 change the value of call-2.

From the example you gave, you are safe.

As for your question about static fields, each caller can see the same static fields, since those are shared between instances. Keep that in mind while programming!

Dekker500
Okay, what I am trying to understand from your statement is in where the static variables are one copy? For all the clients in app domain or just one session?
Braveyard
Static is for the application pool for the server, and therefore all users of the web server would see the same value. Note that if you are running your app on a web farm, the users MAY see different versions, since each request may get served from a different server/farm... Careful coding (persistence to a database) is recommended...
Dekker500
Are you sure about the Application Pool? Because an Application Pool can contain a lot of AppDomains so according to your statement, a static field can be shared amongst all the AppDomains which is against to isolation policy of AppDomains.
Braveyard
What I know is something like that : `Applicaton Pool` > `Application` > `AppDomain` > `Threads` and also `ApplicationPool` is running under its `Worker Process`. ApplicationPool and Application is used by IIS but AppDomain is something like WorkerProcess for .NET. Even though an Application can contain many more AppDomain, there is often one-to-one relationship between them.
Braveyard
Thank you for correcting me I should have said domain not pool! As stated in the C# specifications (http://msdn.microsoft.com/en-us/library/ms228593.aspx):5.1.1 Static variables... ceases to exist when the associated application domain ceases to exist.
Dekker500
+1  A: 

This should answer most of you questions http://odetocode.com/Articles/313.aspx

As I understand it, static methods are thread safe, but not static properties

Kirit Chandran
I always love the articles in that website. Thanks for sharing it.
Braveyard