1

I had another question here in the same context. Now I have another query.

In NBuilder, FizzWare.NBuilder namespace contains the interface ISingleObjectBuilder and class SingleObjectBuilderExtension which is container of extension methods of ISingleObjectBuilder.

Now, the extension methods has a generic syntax:

public static ISingleObjectBuilder<T> ExMethod<T,[more types...]>(this ISingleObjectBuilder<T> objectBuilder, [more params])
{
    ((IObjectBuilder<T>)objectBuilder).ExMethod([more params]);
    return objectBuilder;
}

So you can see, although the methods accept ISingleObjectBuilder, it expects the parameter to be an IObjectBuilder which is a sub-interface of ISingleObjectBuilder.

Now consider this senario:

  • I have implemented MySingleObjectBuilder:ISingleObjectBuilder.
  • In my client code I have using FizzWare.NBuilder I can access MySingleObjectBuilder.ExMethod()
  • When I do this, I obviously get an exception because MySingleObjectBuilder is not an IObjectBuilder.

Of course, it would be safer if the Extension Methods were for IObjectBuilder. Now the question is, isn't the safer option be better too?

Community
  • 1
  • 1
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
  • 2
    Call me silly, but most of the time - in my experience - if you're casting away an abstraction.. even to another abstraction, there's something smelly going on in your design.. – Simon Whitehead Aug 14 '12 at 03:07

0 Answers0