Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if using an interface should a class always strictly implement an interface

The better way to ask this question would be an example as follows What are the pros and cons of the 2 approaches? Is one always better than the other or under specific circumstances? If using Approach1, using an interface would be moot right? since anyone can access the public methods anyway?

public interface IDoSomething
{
  void Method1(string operation, User user, string category)
  void Method2(string operation, User user)
  void Method3(string operation)
}

//Approach1
Class A: IDoSomething
{                              
  public void Method1(string operation, User user, string category)
  {
   //do some db logic here...
  }

  public void Method2(string operation, User user)
  {
    Method1(operation, user, "General");
  }

  public void Method3(string operation)
  {
    Method1(operation, User.GetDefaultUser(), "General");
  }
}

OR

//Approach2
Class A: IDoSomething
{                              
  void IDoSomething.Method1(string operation, User user, string category)
  {
   //do some logic here...
  }

  void IDoSomething.Method2(string operation, User user)
  {
    (this as IDoSomething).Method1(operation, user, "General");
  }

  void IDoSomething.Method3(string operation)
  {
    (this as IDoSomething).Method1(operation, User.GetDefaultUser(), "General");
  }
}
like image 853
EndlessSpace Avatar asked Sep 28 '10 16:09

EndlessSpace


2 Answers

The phrase you're looking for to describe the second approach is explicit interface implementation. Personally, I wouldn't use it most of the time. It's useful it you need to implement different interfaces with the same member signature in different ways, or if you want to restrict some members to only be visible when dealing with an expression of the interface type... but it can be a pain if for whatever reason your caller has the concrete type, and needs to call some type-specific methods and some interface methods. You can't even see explicitly implemented methods within the same class without casting this to the interface type.

Explicit interface implementation also means that you can't override the interface methods - you'd have to reimplement the interface within any subclasses. Basically it ends up being quite complicated - so I'd avoid it if possible.

One final note: explicit interface implementation also plays badly with dynamic in C# 4.

like image 132
Jon Skeet Avatar answered Sep 19 '22 11:09

Jon Skeet


Approach 1 is the natural way of implementing interfaces in C#. If you write a library, this is what your customers will expect; if you use the class yourself, you save yourself a lot of casting when calling the methods.

Use Approach 2 (explicit interface implementation) if you have a good reason for doing so. Good reasons could include

  • implementing multiple interface sharing the same method signature (but requiring different implementations),
  • method names that make sense for the interface but could be misleading for your class.
like image 45
Heinzi Avatar answered Sep 22 '22 11:09

Heinzi