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.
- Keep it
IFooCollection- I don't like this because it looks like an interface. - Name it
FooCollection- I don't like this because it is not a collection of foos. - Turn it into
FooBaseCollectionbecause all implementations ofIFooderive fromFooBase- I don't like this because this might not be true forever. - 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. - 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.
-
I like
FooCollectionyou have a collection of the conceptual object "Foo" even if there is not an actualFooclass or interface. This is in keeping withIFoois an interface of a "Foo" even if there is noFooclass.SpecialFoowould be a special kind of "Foo" even though there is noFooclass.I definitely agree that
IFooCollectionis wrong because of the implied interface. -
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.
-
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.
-
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"
-
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.
-
IMO #2 is the correct naming, though I would also suggest FooList since you're deriving from a List.
-
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.
0 comments:
Post a Comment