Skip to main content
deleted 96 characters in body
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

Is there a way to unify or refactor two methods that do basically the same but the difference being of handling a type of object or a collection of the same type of object?

The following is an implementation of two methods that have very similar behavior. They handle a cache for an API, so when someone asks for a value or a set of values, it verifies that the entry is in cache already. If not, they call the method that will return from an external API: a value or a set of values. Finally the methods, update the cache and return the value/values.

    public async Task<T> GetValue<T>(string cacheKey, Func<Task<T>> getItem, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out T cacheEntry))
        {
            return cacheEntry;
        }
        else
        {
            cacheEntry = await getItem();

            return SetValue(cacheKey, cacheEntry, memoryCacheEntryOptions);
        }
    }

    public async Task<IList<T>> GetValues<T>(string cacheKey, Func<Task<IList<T>>> getItems, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out IList<T> cacheEntries))
        {
            return cacheEntries;
        }
        else
        {
            cacheEntries = await getItems();

            return SetValues(cacheKey, cacheEntries, memoryCacheEntryOptions);
        }
    }

Is there a way to unify or refactor two methods that do basically the same but the difference being of handling a type of object or a collection of the same type of object?

The following is an implementation of two methods that have very similar behavior. They handle a cache for an API, so when someone asks for a value or a set of values, it verifies that the entry is in cache already. If not, they call the method that will return from an external API: a value or a set of values. Finally the methods, update the cache and return the value/values.

    public async Task<T> GetValue<T>(string cacheKey, Func<Task<T>> getItem, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out T cacheEntry))
        {
            return cacheEntry;
        }
        else
        {
            cacheEntry = await getItem();

            return SetValue(cacheKey, cacheEntry, memoryCacheEntryOptions);
        }
    }

    public async Task<IList<T>> GetValues<T>(string cacheKey, Func<Task<IList<T>>> getItems, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out IList<T> cacheEntries))
        {
            return cacheEntries;
        }
        else
        {
            cacheEntries = await getItems();

            return SetValues(cacheKey, cacheEntries, memoryCacheEntryOptions);
        }
    }

Is there a way to unify or refactor two methods that do basically the same but the difference being of handling a type of object or a collection of the same type of object?

The following is an implementation of two methods that have very similar behavior. They handle a cache for an API, so when someone asks for a value or a set of values, it verifies that the entry is in cache already. If not, they call the method that will return from an external API: a value or a set of values. Finally the methods, update the cache and return the value/values.

public async Task<T> GetValue<T>(string cacheKey, Func<Task<T>> getItem, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
{
    if (_memoryCache.TryGetValue(cacheKey, out T cacheEntry))
    {
        return cacheEntry;
    }
    else
    {
        cacheEntry = await getItem();

        return SetValue(cacheKey, cacheEntry, memoryCacheEntryOptions);
    }
}

public async Task<IList<T>> GetValues<T>(string cacheKey, Func<Task<IList<T>>> getItems, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
{
    if (_memoryCache.TryGetValue(cacheKey, out IList<T> cacheEntries))
    {
        return cacheEntries;
    }
    else
    {
        cacheEntries = await getItems();

        return SetValues(cacheKey, cacheEntries, memoryCacheEntryOptions);
    }
}
Post Reopened by Peilonrayz
Post Closed as "Not suitable for this site" by forsvarir, Peilonrayz
edited title
Link

Is it possible to refactor these two similar methods that handle T and List<T>?

Source Link

Is it possible to refactor two similar methods that handle T and List<T>?

Is there a way to unify or refactor two methods that do basically the same but the difference being of handling a type of object or a collection of the same type of object?

The following is an implementation of two methods that have very similar behavior. They handle a cache for an API, so when someone asks for a value or a set of values, it verifies that the entry is in cache already. If not, they call the method that will return from an external API: a value or a set of values. Finally the methods, update the cache and return the value/values.

    public async Task<T> GetValue<T>(string cacheKey, Func<Task<T>> getItem, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out T cacheEntry))
        {
            return cacheEntry;
        }
        else
        {
            cacheEntry = await getItem();

            return SetValue(cacheKey, cacheEntry, memoryCacheEntryOptions);
        }
    }

    public async Task<IList<T>> GetValues<T>(string cacheKey, Func<Task<IList<T>>> getItems, MemoryCacheEntryOptions memoryCacheEntryOptions = null)
    {
        if (_memoryCache.TryGetValue(cacheKey, out IList<T> cacheEntries))
        {
            return cacheEntries;
        }
        else
        {
            cacheEntries = await getItems();

            return SetValues(cacheKey, cacheEntries, memoryCacheEntryOptions);
        }
    }