简体   繁体   中英

Array string multidimension index

I am looking for an advise on the right data structure to use. I am sure this should be obvious but I cannot manage to find the right answer. I need to store SOAP service instance into a data structure which will allow me to access them like:

AXLAPIService myInstance = instances["DEV"]["EU"]

// table values
["DEV"]["EU"][instance]
["DEV"]["AM"][instance]
["PROD"]["EU"][instance]
["PROD"]["AM"][instance]

// Data to store
[String][String][Object]

I thought about Tuple and dictionary but is a bit unsure about if I can access them using multiple indexes based on string. Anyone could help to declare the right structure to achieve this? Thanks

Dictionary with composite key is an option. Tuple is cheap way to create such keys that already have all necessary comparison/hashing methods:

 Dictionary<Tuple<string,string>, Instance> instances = new ....;

 instances.Add(Tuple.Create("DEV", "EU"), euDevInstance);....


 var find = instances[Tuple.Create("PROD", "AM");

Note: If you decide to go this route creating custom class with necessary fields and comparison/hashing methods may make your code more readable.

Conceptually it could be something like this:

class ConcreteAXLAPIService<T>
{
  private Dictionary<string, T> servicePool;

  public T this[string id]
  {
    get
    {
        return servicePool[id];
    }
  }
}


class MyService<T>
{
    private Dictionary<string, T> methodPool;

    public T this[string id]
    {
      get
      {
          return methodPool[id];
      }
    }
}

USAGE:

var api = new ConcreteAXLAPIService<MyService>();
...
var apiMethod = api["ServiceName"]["MethodName"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM