Which is more efficient and thread-safe? static or instant classes?
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 [Broker] scenario will scale best if used in a web environment with roughly 100 different roles and over a thousand users.
NOTE. Feel free to use an alternative approach.
a source to share
You can mess up streaming by using either static classes or instance classes. However, the key to properly handling threads is to ensure that no two threads are trying to access the same resource at the same time.
With static classes, by definition, there is only one copy of it around the idea that all threads should share well.
In instance classes, you can create one instance for each thread. If you ensure that each thread only accesses its own instance, AND that the properties and methods of the instance do not have access to other shared resources, they must be thread safe.
In your case, I don't see anything changing in the classes after initialization. If you make sure to initialize your static classes in a thread-safe way (as appears to be here), the static option should be thread-safe (with read-only access to variables) and will be slightly faster because you don't have the overhead of creating and deleting copies.
a source to share
Both approaches have their own problems. My usual approach is to go for instance classes (because they are easier to test as well), but you can define a static instance of that class if you want data streamed. Anyway, if your data gets split, you probably want to explore the classReaderWriterLockSlim
.
a source to share
Instance classes will be more thread safe since each thread can have one instance per thread. Static classes, on the other hand, you can only have one, so if you need your threads to communicate, make sure you implement some kind of blocking mechanism so that they play well with each other. Also indicate which efficiency context you require speed or resource consumption.
a source to share
There is no absolute safer flow if you are not going to access the shared data, there is no difference, and if you are, there is no difference. But it is more likely that the static classes used shared data because they are static and almost everything is shared for them. so use instant classes. Like Martin Fowler Singleton's pattern, even if you want something static, it's better to make it single because it's more flexible.
a source to share