Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension interface patterns

Tags:

The new extensions in .Net 3.5 allow functionality to be split out from interfaces.

For instance in .Net 2.0

public interface IHaveChildren {     string ParentType { get; }     int ParentId { get; }      List<IChild> GetChildren() } 

Can (in 3.5) become:

public interface IHaveChildren {     string ParentType { get; }     int ParentId { get; } }  public static class HaveChildrenExtension {     public static List<IChild> GetChildren( this IHaveChildren ) {         //logic to get children by parent type and id         //shared for all classes implementing IHaveChildren      } } 

This seems to me to be a better mechanism for many interfaces. They no longer need an abstract base to share this code, and functionally the code works the same. This could make the code more maintainable and easier to test.

The only disadvantage being that an abstract bases implementation can be virtual, but can that be worked around (would an instance method hide an extension method with the same name? would it be confusing code to do so?)

Any other reasons not to regularly use this pattern?


Clarification:

Yeah, I see the tendency with extension methods is to end up with them everywhere. I'd be particularly careful having any on .Net value types without a great deal of peer review (I think the only one we have on a string is a .SplitToDictionary() - similar to .Split() but taking a key-value delimiter too)

I think there's a whole best practice debate there ;-)

(Incidentally: DannySmurf, your PM sounds scary.)

I'm specifically asking here about using extension methods where previously we had interface methods.


I'm trying to avoid lots of levels of abstract base classes - the classes implementing these models mostly already have base classes. I think this model could be more maintainable and less overly-coupled than adding further object hierarchies.

Is this what MS has done to IEnumerable and IQueryable for Linq?

like image 908
Keith Avatar asked Aug 11 '08 18:08

Keith


People also ask

What are extension interfaces?

The Extension Interface design pattern allows multiple interfaces to be exported by a component, to prevent bloating of interfaces and breaking of client code when developers extend or modify the functionality of the component.

What are extension objects?

Extension objects are used to extend the functionality of style sheets. Extension objects are maintained by the XsltArgumentList class. The following are advantages to using an extension object rather than embedded script: Provides better encapsulation and reuse of classes.

Is interface a design pattern?

Interfaces are unrelated to Design Patterns. They are about Object-Orientation. In OO, the type of an object is defined by its behavior, i.e. by the protocol it speaks (its methods). Classes in languages like Java and C#, however, also define representation (fields) in addition to behavior (methods).


1 Answers

Extension methods should be used as just that: extensions. Any crucial structure/design related code or non-trivial operation should be put in an object that is composed into/inherited from a class or interface.

Once another object tries to use the extended one, they won't see the extensions and might have to reimplement/re-reference them again.

The traditional wisdom is that Extension methods should only be used for:

  • utility classes, as Vaibhav mentioned
  • extending sealed 3rd party APIs
like image 142
Jon Limjap Avatar answered Nov 02 '22 22:11

Jon Limjap