views:

183

answers:

7

The situation is as follows.

public interface IFoo { }

public abstract class FooBase : IFoo { }

Now I need a collection of IFoo with some additional methods.

public class IFooCollection : List<IFoo>
{
   public void UsefullMethod() { }
}

The problem is that IFooCollection looks like an interface while it is a class. The options are the following.

  1. Keep it IFooCollection - I don't like this because it looks like an interface.
  2. Name it FooCollection - I don't like this because it is not a collection of foos.
  3. Turn it into FooBaseCollection because all implementations of IFoo derive from FooBase - I don't like this because this might not be true forever.
  4. Don't create the class at all but provide extension methods for IList<IFoo> because there are only a hand full methods - I don't like this because changing the code because you cannot find a name for a class ... yes, that is nasty.
  5. Something I did not think about or forgot to write it down - I hope I will like it!

So what would you do? Is there a naming convention I missed? We are basicaly using this Microsoft .NET Library Standards.

UPDATE

The code will not become widespread - it is just inside a GUI tool to put some data into a server. So I don't care about using the methods with other collections or overlooking the methods.

+10  A: 

I like FooCollection you have a collection of the conceptual object "Foo" even if there is not an actual Foo class or interface. This is in keeping with IFoo is an interface of a "Foo" even if there is no Foo class. SpecialFoo would be a special kind of "Foo" even though there is no Foo class.

I definitely agree that IFooCollection is wrong because of the implied interface.

Alex B
A: 

I actually like #4 more than building your own collection type, because ultimately users are going to want to stuff their IFoo-implementing objects into their own lists and other collections. This way, those collections will work as expected.

Joel Coehoorn
A: 

You have identified most of the choices really.

The only additional one I could think of is CollectionOfIFoo, but that is not in line with the conventions.

Id probably go with IFooCollection.

Jason Coyne
A: 

Personally, I like the idea of using extension methods. If you are worried about people being able to find the extension methods easily, just put them in a static class in the same code file as the IFoo interface. Or create an "IFooExtensions" class in a separate file in the same namespace to make it easy to spot when people are looking at "IFoo"

Eric Petroelje
+7  A: 

FooCollection - it's not obvious with 'Foo' as Foo has no meaning, so it's difficult to conceptualise. Try it with a 'real' class/interface name and it makes more sense - e.g.

public class ErrorHandlerCollection : List<IErrorHandler>
{
  public void PublishErrors(){//...}
}

This makes sense because an ErrorHandlerCollection is a collection of error handlers. Anything that implements IErrorHandler IS an error handler, so anything in the ErrorHandlerCollection will be an error handler.

Steve Willcock
A: 

IMO #2 is the correct naming, though I would also suggest FooList since you're deriving from a List.

Peter Lillevold
A: 

How about CollectionOfIFoo?

Personally, I don't like the convention of prefixing interfaces with 'I' - it's basically a form of the bad kind of hungarian notation, and this is an example of why it's bad.

Michael Borgwardt