To the refactor so that both the NameValueCollection parameter and the KeyValueConfigurationCollection as the parameter?

Code example:

EDITED : A clarified example. Sorry for any confusion.

        using System.Collections.Specialized;
        using System.Configuration;

    ...

        // get collection 1

        NameValueCollection c1 = ConfigurationManager.AppSettings;

        // get collection 2

        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = "c:\\SomeConfigFile.config";
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        KeyValueConfigurationCollection c2 = config.AppSettings.Settings;

        // do something with collections

        DoSomethingWithCollection(c1);
        DoSomethingWithCollection(c2);
...

    private void DoSomethingWithCollection(KeyValueConfigurationCollection c)
    {
        foreach(KeyValueConfigurationElement el in c)
        {
            string key = el.Key;
            string val = el.Value;
            // do something with key and value
        }
    }

    private void DoSomethingWithCollection(NameValueCollection c)
    {
        for(int i=0; i < c.Count; i++)
        {
            string key = c.GetKey(i);
            string val = c.Get(i);
            // do something with key and value
        }
    }

There are currently two versions of DoSomethingWithCollection for creating either NameValueCollection or KeyValueConfigurationCollection.

Is there a cleaner way to do this, so there is only one version of DoSomethingWithCollection?

Thank.

+3
source share
3 answers

, DoSomethingWithCollection - 1) ICollection/IEnumerable, . 2) , . .

: , .

delegate void KeyValueItemParser(object item, out string key, out string value);

void DoSomethingWithCollection(ICollection items, KeyValueItemParser parser)
{

   string key, value
   foreach(object item in items)
   {
      parser(item, out key, out value);
      // do whatever you want to with key & value
   }
}

. 2: , . DoSomethingWithCollection, - , - . . - , / NamedValuePair/KeyValuePair, -, .

interface IKeyValuePairCollection
{
  int Count {get; }
  KeyValuePair<string, string> Item(int index) { get; }
}

class NamedValueCollectionWrapper : IKeyValuePairCollection
{

   private NamedValueCollection _inner;

   public NamedValueCollectionWrapper(NamedValueCollection target) 
   {
     -inner = target;
   }

   // roll out ur implementation to IKeyValuePairCollection
}

class KeyValueConfigurationCollectionWrapper : IKeyValuePairCollection
{
  ...
}

IMO, .

+4

DoSomethingWithCollection, NameValueCollection, - KeyValueConfigurationCollection. , , , .

+1

Since both the collection NameValueCollectionand KeyValueConfigurationCollectionrealize how ICollection, and IEnumerableyou can do the following:

private void DoSomethingWithCollection(ICollection collection)
// private void DoSomethingWithCollection(IEnumerable collection)
{
    NameValueCollection nv;
    KeyValueConfigurationCollection kvc;
    if ((nv = collection as NameValueCollection) != null)
    {
        // do stuff directly or call iterate and call 3rd method
    }
    else if ((kvc = collection as KeyValueConfigurationCollection) != null)
    {
        // do stuff directly or call iterate and call 3rd method
    }
}
0
source

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


All Articles