C# - Which is more efficient and thread safe? static or instant classes?
- by Soni Ali
Consider the following two scenarios:
//Data Contract
public class MyValue
{
}
Scenario 1: Using a static helper class.
public class Broker
{
private string[] _userRoles;
public Broker(string[] userRoles)
{
this._userRoles = userRoles;
}
public MyValue[] GetValues()
{
return BrokerHelper.GetValues(this._userRoles);
}
}
static class BrokerHelper
{
static Dictionary<string, MyValue> _values = new Dictionary<string, MyValue>();
public static MyValue[] GetValues(string[] rolesAllowed)
{
return FilterForRoles(_values, rolesAllowed);
}
}
Scenario 2: Using an instance class.
public class Broker
{
private BrokerService _service;
public Broker(params string[] userRoles)
{
this._service = new BrokerService(userRoles);
}
public MyValue[] GetValues()
{
return _service.GetValues();
}
}
class BrokerService
{
private Dictionary<string, MyValue> _values;
private string[] _userRoles;
public BrokerService(string[] userRoles)
{
this._userRoles = userRoles;
this._values = new Dictionary<string, MyValue>();
}
public MyValue[] GetValues()
{
return FilterForRoles(_values, _userRoles);
}
}
Which of the [Broker] scenarios will scale best if used in a web environment with about 100 different roles and over a thousand users.
NOTE: Feel free to sugest any alternative approach.