C# - Which is more efficient and thread safe? static or instant classes?

Posted by Soni Ali on Stack Overflow See other posts from Stack Overflow or by Soni Ali
Published on 2010-04-19T16:19:31Z Indexed on 2010/04/19 16:33 UTC
Read the original article Hit count: 364

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about thread-safety