How can we merge similar getters together?

2019-07-30 06:09发布

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;
        }
    }

}

标签: c# oop
3条回答
姐就是有狂的资本
2楼-- · 2019-07-30 07:01

Helper method:

static IStorageProvider GetStorageProvider(ref IStorageProvider field, string name)
{
    lock(_lock)
    {
        if(field == null)
        {
            var storageProviderType = Type.GetType(name);
            field = (IStorageProvider)Activator.CreateInstance(storageProviderType);
        }
    }
    return field;
}

Usage:

private static IStorageProvider BAStorageProvider
{
    get
    {
        return GetStorageProvider(
            ref _BAstorageProvider,
            BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName);
    }
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-30 07:09

A method like this:

public void GetProvider(string typeName, ref IStorageProvider provider)
{
    if (provider == null)
    {
        lock (_lock)
        {
            if (provider == null)
            {
                var storageProviderType = Type.GetType(typeName);
                provider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
            }
        }
    }
}

should do the trick. Then you call it like this:

private static IStorageProvider BAWP8StorageProvider
{
    get
    {
        GetProvider(BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName, ref _BAWP8storageProvider);
        return _BAWP8StorageProvider;
    }
}

Though I would personally lose the _BAWP8StorageProvider class members, and just use the properties instead (that would remove the ugly ref parameters too).

查看更多
We Are One
4楼-- · 2019-07-30 07:11
private static IStorageProvider GetProvider(string typeName)
{
    return (IStorageProvider)Activator.CreateInstance(Type.GetType(typeName));
}

private static readonly Lazy<IStorageProvider> _BAstorageProvider =
    new Lazy<IStorageProvider>(
        () => GetProvider(
            BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName),
        LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly Lazy<IStorageProvider> _BAWP8storageProvider =
    new Lazy<IStorageProvider>(
        () => GetProvider(
            BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName),
        LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly Lazy<IStorageProvider> _M4MstorageProvider =
    new Lazy<IStorageProvider>(
        () => GetProvider(
            BLL.Providers.ConfigurationProvider.Instance.M4MStorageProviderTypeName),
        LazyThreadSafetyMode.ExecutionAndPublication);

private static IStorageProvider BAStorageProvider
{
    get
    {
        return _BAstorageProvider.Value;
    }
}

private static IStorageProvider BAWP8StorageProvider
{
    get
    {
        return _BAWP8storageProvider.Value;
    }
}

private static IStorageProvider M4MStorageProvider
{
    get
    {
        return _M4MStorageProvider.Value;
    }
}

(But I'd recommend using Dependency Container)

查看更多
登录 后发表回答