How can I make this method more general?

I have a LoadToDictionary method. It takes a dictionary and a file path. As it is now time to look something like this: void LoadToDictionary(Dictionary<string, string> dict, string filePath. Then inside, it will have a lot of code to extract the contents of the file in filePath into the dictionary. I want to make it more general so that it can, say, take a dictionary with int instead of it and the ability to change the analysis code in the method. Should I mark this method and then override it? Is there another way?

+3
source share
2 answers

Premature commonality gives premature optimization of the run for money as the root of evil. Conventionality is expensive , and expensive code has to be justified thanks to its clear advantages.

Therefore, I would use increased generality in your method based on specific scenarios. I can think of many ways to make your method more general:

  • Do not take a string dictionary into a string; take a dictionary of, say, strings for an arbitrary type.

  • Do not take Dictionary<...>; take it IDictionary<...>.

  • Do not use a dictionary. Take Action<K, V>, the action of which may be to introduce an element into the dictionary.

  • Do not accept the file name. In any case, you simply turn the file name into a stream, so start the stream first. Ask the caller to open the stream for you.

  • . , IEnumerable<...> .

? , T, K V. ? , , :

static void MakeMap<T, K, V>(this IEnumerable<T> sequence, Action<K, V> mapWriter, Func<T, K> keyExtractor, Func<T, V> valueExtractor)
{
    foreach(var item in sequence)
        mapWriter(keyExtractor(item), valueExtractor(item));
}

, , . , - . , ? ? . , , ? , .

? , ? . , , , . ? ? , ? , ? , :

public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector,
IEqualityComparer<TKey> comparer) { ... }

, ToLookup(). , , , .

+36

:
1) , , , ?

2) , " ".

, Reflection gen. , , .

, , , .

:

void LoadToDictionary(Dictionary<string, string> dict, string filePath)
{ ... code here ... }

//to have a LoadToDictionary method which accepts Dictionary<int, int>
void LoadToDictionary(Dictionary<int, int> dict, string filePath)

//To change the processing logic, you either need a new method name, 
// or to override the original in an inheriting class

void AlternateLoadToDictionary(Dictionary<string, string> dict, string filePath)
override void LoadToDictionary(Dictionary<string, string> dict, string filePath)

, . LoadDictionary, .

void LoadToDictionary(Dictionary<string, string> dict, string filePath)
{
    string[] fileLines;

    if([yourBaseLineCondition])
    fileLines = LoadDataFromFile(filePath, false);

    else fileLines = LoadDataFromFile(filePath, true); 
}

string[] LoadDataFromFile(string filePath, bool altLogic)
{
    if(altLogic) return LoadDataFromFileAlt(filePath)
    else
    { ... your logic ... }
}

string[] LoadDataFromFileAlt(string filePath)
{ ... your alt logic... }

. , YAGNI, . LoadToDictionary<myCustomObject, myCustomObject>, .

+1

Source: https://habr.com/ru/post/1761780/


All Articles