views:

53

answers:

3

If I had a CarsDataStore representing a table something like:

Cars
--------------
Ford | Fiesta
Ford | Escort
Ford | Orion
Fiat | Uno
Fiat | Panda

Then I could do

IEnumerable<Cars> fords = CarsDataStore["Ford"];

Is this a bad idea? It's inconsistent with the other datastore objects in my api (which all have a single column PK indexer), and I'm guessing most people don't expect an indexer to return a collection in this situation.

+2  A: 

Yes, i think it's a bad idea, specially, if it's inconsistent with the rest of the api. Consistency is a key thing and you should always strive to be consistent.

Galilyou
+2  A: 

I wouldn't expect an indexer to return a collection but then the compiler would tell me that if I ran into any problems.

However I would personally implement something more along the lines of

IEnumerable<Cars> fords = CarsDataStore.GetCarsByType("Ford");
Nick Allen - Tungle139
+2  A: 

Change your primary data structure and your issue goes away (syntax probably isn't perfect):

Dictionary<string, List<string>> cars =
    new Dictionary<string, List<string>>();

cars.Add("Ford", { "Fiesta", "Escort", "Orion" });
cars.Add("Fiat", { "Uno", "Panda" });

IEnumerable<string> fordCars = cars["Ford"];

Of course, you would have to change string to Car, but the idea would be the same.

Justin Niessner