This question may be too vague or broad, but i figured i'd give it a shot. I've inherited a large .NET project and have run into some things i've not seen before. The most pressing question i have, is what would be the difference between these two declarations? Both of them work, and both types are used in existing code, but i'm wondering if one should be used over the other for performance or security reasons.
var mgr = ManagerFactory.GetInstance<CustomerNotificationManager>();
vs.
CustomerNotificationManager cNotificationMgr = new CustomerNotificationManager();
Both result in an instance of the CustomerNotificationManager
class that can be used for any methods within.
Let me know if you need any more info to (hopefully) answer my question. Also, if this question is 'answerable', feel free to suggest a better title.
public class ManagerFactory
{
private static bool singleton = false;
private static Dictionary<string, ManagerBase> instanceHolder = new Dictionary<string, ManagerBase>();
public static bool Singleton
{
get { return ManagerFactory.singleton; }
set { ManagerFactory.singleton = value; }
}
public static T GetInstance<T>() where T : ManagerBase, new()
{
if (singleton)
{
return getSingletonInstance<T>();
}
else
{
return new T();
}
}
private static T getSingletonInstance<T>() where T : ManagerBase, new()
{
lock (instanceHolder)
{
Type genericType = typeof(T);
if (instanceHolder.ContainsKey(genericType.ToString()))
return instanceHolder[genericType.ToString()] as T;
else
{
var instance = new T();
instanceHolder.Add(genericType.ToString(), instance);
return instance;
}
}
}
}