-2

It pretty much makes sense to me of using interface with method signature/events and so on.

But, I am not able to find what's the use case for using interfaces with just properties. ( I know properties are methods under the hood and they encapsulate and you can write some code on get/set accessor and such.

What I'm behind is the use case for interface with properties which acts like fields. (just get and set values).

For example, if I have interface with methods, I could use different implementations at run time, has advantages using polymorphism, pass different implementation based on interface contract, and it's easier for me to mock for unit tests. But if I have a class and I know it will just contain properties (like DTO). Why would I want to create an interface for DTO class? Is there any use case?

I tagged java because this is not a technical question and they have mutator method similar to get/set accessor.

Thanks.

ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • 2
    Why did you tag this with java? – Elliott Frisch Sep 11 '15 at 02:38
  • @ElliottFrisch Maybe they mean bean properties - `getXXX()`, `setXXX()` – jdphenix Sep 11 '15 at 02:39
  • In Java, such use cases include [Value Object](https://en.wikipedia.org/wiki/Value_object) and/or [Transfer Object](http://www.oracle.com/technetwork/java/transferobject-139757.html). But I think it frequently shows up in er modeling ([entity-representation](http://web.cse.ohio-state.edu/~gurari/course/cse670/cse670Ch2.xht)). – Elliott Frisch Sep 11 '15 at 02:41
  • 1
    Give an example of such an interface. If you cannot find any examples then it is apparently not useful. If you can find some examples then we can consider the examples on their individual merits. – Eric Lippert Sep 11 '15 at 02:44
  • 1
    I agree that you need a very good reason to have a properties-only interface. There's usually not any reason for having multiple implementations of a data structure without behaviors so a concrete class is well-suited for the task. However, it may be that an object not only has to play the role of that data structure, but has to play other roles as well. – plalx Sep 11 '15 at 18:48

3 Answers3

1

It turns out that there are quite a few interfaces in the framework that only contain properties, e.g. System.IAsyncResult. You may find other use cases by browsing through MSDN.

IMHO, properties have nothing special comparing to methods. You may always converting a R/W property to a pair of Get/Set methods and converting a R/- property to a Get method or vice versa. The decision for an interface consisting of purely properties should be made in the same process as that for a "normal" interface.

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
  • I know the interface cannot contain fields. I'm just curios on the use case for creating an interfaces with just properties (without any logic in the get and set method in the implementor, basically just like fields) – ANewGuyInTown Sep 11 '15 at 02:50
  • Interfaces do not have logic. A property in an interface only requires implementing types to have public properties exposed, and are not auto implemented properties. – jdphenix Sep 11 '15 at 02:56
  • @ZammyPage Updated. There turns out to be quite a few interfaces with only properties in the framework. – Dante May Code Sep 11 '15 at 03:09
0

Properties in interfaces have the same use cases as other parts of interfaces - they provide a common interface to interact with a type implementing that interface.

Note that properties in interfaces are different from properties in classes.

interface IInterface
{
    int Foo { get; }
}

class Class
{
    int Foo { get; set; } 
}

In this code, IInterface.Foo is not an auto-implemented property, whereas Class.Foo is.

Examples in the BCL include:

// Get the count of items of any ICollection implementation, 
// including (not limited to) List<T>, Dictionary<TKey, TValue>, 
// and HashSet<T>
int ICollection<T>.Count { get; }

// gets the current element of any implementation of IEnumerator<T>
T IEnumerator<T>.Current { get; }
jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • I can see the technical differences and usages. What I want to know is with regard to real world usages , what benefits we might get with an interface with just properties. – ANewGuyInTown Sep 11 '15 at 02:56
0

Interfaces play an important role in many design patterns line Inversion of Control, Dependency Inversion, and many other . we may use inheritance for the same, but if there is a violation of is-a relationship then we may go for realisation.

Abdul Rahman K
  • 664
  • 5
  • 16