I have read the posts regarding when to use static classes and when it is recommended to use instance classes. However, I have the impression my example below somewhat falls in between:
- No class instances are needed, stored state is shared among members within the AppDomain.
- State should be accessible from different class instances within the AppDomain.
- No abstractions or overrides are ever needed.
So, my question is: Should I go ahead and use it as static or is it better to use a singleton concept?
public static class SubscriptionManager
{
private static Dictionary<string, string> Repository { get; set; }
static SubscriptionManager()
{
Repository = new Dictionary<string, string>();
}
public static void Subscribe(string key, string value)
{
if (Repository.ContainsKey(key))
Repository[key] = value;
else
Repository.Add(key, value);
}
public static void Unsubscribe(string key, string value)
{
if (Repository.ContainsKey(key))
Repository.Remove(key);
}
public static string GetSubscription(string key)
{
if (Repository.ContainsKey(key))
return Repository[key];
else
return "";
}
}
Remember large static classes can take up a lot of memory so avoid them when not necessary. in this case i would suggest you to go with static classes. It is better this way.