1

I am creating a profile page wizard so the user would have quite a few select boxes to validate against.

The way I have it set up the value of my select boxes are keys to an string array.

For example:

string[] HairColor = {"blonde", "amber", "black", "platinum"}.

Would there be a threading issue if I create a helper static class, and uses a method such as:

public static string GetHairColor(int key)
{
 string[] HairColor = {"blonde", "amber", "black", "platinum"}.
 return HairColor[key];
}

The reason why I want to return a string, is because I would rather limit the amount I would have to validate against, if the number returns a value then, I call a db or cache save. So instead of the user inserting there string values, I am inserting it for them based on the number they pass in.

My question is if users were concurrently checking against this method, would there be an issue with threading?

*note I am not passing the array into the view, I am just trying to quickly check the user isn't passing in a number that doesn't exist, if it does grab it and persist it. Thanks for your help everyone.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Denis Cekic
  • 155
  • 2
  • 11
  • You could make a custom HtmlHelper extension to do this for you. – Jasen Nov 21 '15 at 21:17
  • Hey thanks for your reply, I just looked over html helpers. It seems like this is geared toward the manipulation of the view. For example success or error messages. Sorry if I misunderstood, but It isn't the type of validation I was aiming for. I just wanted to check it on the server side really quick before persisting the selection. Also there are many selections hair color, body type, height, eye color etc. So checking against that example and threading is my main concern right now. – Denis Cekic Nov 21 '15 at 21:27
  • There isn't really any good reason to use arrays, you should take advantage of IEnumerable classes. – Erik Philips Nov 21 '15 at 21:30
  • @Erik I will have a good number of selections for the user to make about there details(hair color, eye color etc..) Could you explain why would I choose to use an your method as opposed to the one I exampled? I am only asking because If it's a better way and wouldn't cause threading issues, I would gladly accept it as the correct answer. – Denis Cekic Nov 21 '15 at 21:44
  • [Adding validation to the model](http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc3/cs/adding-validation-to-the-model). Take a while and read up on [http://www.asp.net/mvc](http://www.asp.net/mvc). – Erik Philips Nov 21 '15 at 21:47

2 Answers2

1

note I am not passing the array into the view, I am just trying to quickly check the user isn't passing in a number that doesn't exist, if it does grab it and persist it.

Then don't let the user pass an invalid number, that is the best user design. Make it a drop down.

As a side note:

public static string GetHairColor(int key)
{
  string[] HairColor = {"blonde", "amber", "black", "platinum"}.
  return HairColor[key];
}

Will throw an IndexOutOfRangeException if that is all your validation does.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • It is a drop down. The key of the drop down is a number. The key also holds the value to the hair color. If the user decided to add a field attempting to screw with the server, the key they passed in wouldn't work because it would get checked (i.e is it an integer, and if it exists). My only concern was if a few users at the same time checked against that array would it cause some threading issue. – Denis Cekic Nov 21 '15 at 21:40
  • First, if someone is messing with your server, server side validation is the **only** way to go, there is no reason for client side validation. Secondly, there is no threading issues with the code I pasted from your question. – Erik Philips Nov 21 '15 at 21:43
  • Sorry Erik I think you misunderstood, the above code is server side. So I am only doing server side validation. As I said I am only concerned if my server side code would have threading issues if users are concurrently hitting that method. – Denis Cekic Nov 21 '15 at 21:46
  • 1
    That *specific* method is thread safe as it stands. I posted a comment on your question, that is best-practices for model validation. – Erik Philips Nov 21 '15 at 21:49
  • I appreciate it, I just wanted to know if that specific method was thread safe so I can move forward. I understand that I might have posted a slightly misleading question because of the complexity of what the whole profile class will be doing, but thanks for clearing that up. – Denis Cekic Nov 21 '15 at 21:53
0

Because you are using a static method you won't have any threading issues because all of the code accessing the static value are operating in there own sessions. The static value is part of the main application thread an has no effect on any other thread or session.

Steve Holdorf
  • 141
  • 1
  • 14
  • *Because you are using a static method you won't have any threading issues* is extremely misleading. [Static methods aren't inherently thread-safe](http://stackoverflow.com/questions/1090650/are-static-methods-thread-safe). – Erik Philips Nov 21 '15 at 21:49