We have 3 classes responsible for handling Images. The boss asked me to merge it. I merged it in one file, but he said it is the same thing, we need to merge it in on general logic. How can we merge these 3 getters in a general one?? they are differ only in the parameter type which is passed to Type.GetType(). we need to use one generic attribute with one generic getter.
public class BonusImageHandler
{
private static IStorageProvider _BAstorageProvider;
private static IStorageProvider _BAWP8storageProvider;
private static IStorageProvider _M4MstorageProvider;
private static readonly object _lock = new object();
private static IStorageProvider BAStorageProvider
{
get
{
lock (_lock)
{
if (_BAstorageProvider == null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName);
_BAstorageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _BAstorageProvider;
}
}
private static IStorageProvider BAWP8StorageProvider
{
get
{
lock (_lock)
{
if (_BAWP8storageProvider == null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName);
_BAWP8storageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _BAWP8storageProvider;
}
}
private static IStorageProvider M4MStorageProvider
{
get
{
lock (_lock)
{
if (_M4MstorageProvider== null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.M4MStorageProviderTypeName);
_M4MstorageProvider= (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _M4MstorageProvider;
}
}
}
Helper method:
Usage:
A method like this:
should do the trick. Then you call it like this:
Though I would personally lose the _BAWP8StorageProvider class members, and just use the properties instead (that would remove the ugly
ref
parameters too).(But I'd recommend using Dependency Container)