I am working on an ASP.NET MVC application.
I have a ViewModel class for which I am trying to maintain a count in order to generate div ids which are used in a view:
public class ViewModel {
private static uint instanceCount;
private readonly uint id;
public ViewModel(){
id = instanceCount++;
}
public uint DivId
{
get { return id; }
}
}
Am I correct in thinking incrementing and assigning the static member is not threadsafe and thus not safe in a web application?
What is the best way to make this safe?
Thanks.
Am I correct in thinking incrementing and assigning the static member is not threadsafe and thus not safe in a web application?
Yes, you are correct into thinking this.
What is the best way to make this safe?
By synchronizing the access:
public class ViewModel
{
private static uint instanceCount;
private readonly uint id;
private static object _syncRoot = new object();
public ViewModel()
{
lock(_syncRoot)
{
id = instanceCount++;
}
}
public uint DivId
{
get { return id; }
}
}
or simply use a Guid:
public class ViewModel
{
public ViewModel
{
DivId = string.Format("id_{0}", Guid.NewGuid());
}
public string DivId { get; private set; }
}
I have to ask, why you can't just handle this within your view:
public ActionResult Index() {
var models = SomeService.GetModels(); // returns IEnumerable<YourViewModel>
return View(models);
}
// in your view
@for (int i = 1; i == Model.Count(); i++) {
<div id="div-@i">
<!-- your content here -->
</div>
}
You may use Interlocked.Increment instead. However, this works only with an signed integer.
You can use System.Threading.Interlocked.Increment()
method. That's will be right choice in your case. See Interlocked class for more info.
精彩评论