70-565 - Implementing interfaces

Filed under: , , by:

One of the 70-565 exam topics is Designing and Developing an Application Framework and it looks like 25% of all questions are about designing and developing an application framework. If to read what the topic covers, you find that there are questions about defining the interaction between framework components, which is defining data APIs, security APIs, abstract classes, class interfaces, and data contracts. There's been plenty of posts and discussion whether to use interfaces or abstract classes, but I noticed that there isn't enough with regard to implementing an interface, which is whether an interface should be implemented implicitely or explicitely. Those are two choices presented by IntelliSense to implement an interface as shown below.

I have personally noticed that many developers think that you implement an interface explicitely only if a class implements another interface that has the same method or property name so explicit implementation resolves the problem of ambiguity. Although this is true, you can implement explicitely an interface even when there is no ambiguity, but by doing so you introduce new features to the class. Below is an example of an interface and classes that implement that interface (explicitely and implicitely).
[Rules.cs]

NameRule class implicitely implements IRule interface whereas EmailRule does explicit implementation. You can see the difference in the signature of the Evalute method - in NameRule class method is public, in EmailRule class public modified is not valid, hence it is not there plus method name is preceded by interface name - IRule. This difference results in the following limitations within a class that implements an interface explicitely:
  • interface method/property is not accessible from the class object as it were private. In the example above, rule object of type EmailRule cannot call Evaluate method, but has to be cast to the interface type first: (rule as IRule).Evaluate
  • interface method/property cannot be abstract or virtual, which means that a class that explicitely implements an interface must provide the implemention of the method and that implemention cannot be overriden or marked as abstract so it can be provided by classes that inherit from the abstract class
I think those are very important things to remember when designing your APIs and making a decision whether your class should explicitely or implicitely implement the given interface. The vast majority of developers use implicit implementation to overcome the aforementioned limitations but sometimes by explicitely implementing an interface you can hide that fact and yet the full advantage of that within that class. Also because of hiding explicit methods from the class view, you can make your class design cleaner especially if the class impelments an interface with many methods and not all of them are required to be called from the class object. And making your APIs clean and other develop friendly should be the first priority of every developer who designs and develops application frameworks - the first priority of course after meeting component's design requirements.

0 comments: