views:

1564

answers:

9

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the use of an empty interface?

A: 

I'd say its used for "future" reference or if you want to share some objects, meaning you could have 10 classes each implementing this interface.

And have them sent to a function for work on them, but if the interface is empty, I'd say its just "pre"-work.

Filip Ekberg
+2  A: 

In Java, empty interfaces were usually used for "tagging" classes - these days annotations would normally be used.

It's just a way of adding a bit of metadata to a class saying, "This class is suitable for <this> kind of use" even when no common members will be involved.

Jon Skeet
Hm sounds like either a misuse of the concept or a smart idea. Probably both ;-).
Gamecat
Before annotations, it was somewhat reasonable. Now there's no good reason to use it, IMO.
Jon Skeet
+4  A: 

Usually it's to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly.

This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.


@Stimpy77 Thanks! I hadn't thought of it that way. I hope you'll allow me to rephrase your comment in a more general way.

Annotations and attributes have to be checked at runtime using reflection. Empty interfaces can be checked at compile-time using the type-system in the compiler. This brings no overhead at runtime at all so it is faster.

Mendelt
Be wary of the .NET comment, it requires reflection to use attributes, which is slower and does not support the "X is Y" and "X as Y" semantics. You can use empty interfaces in .NET too.
stimpy77
+2  A: 

In java Serializable is the perfect example for this. It defines no methods but every class that "implements" it has to make sure, that it is really serializable and holds no reference to things that cannot be serialized, like database connections, open files etc.

André
And that you bettter add the serialVersionUID
Ubersoldat
+6  A: 

Also known as a Marker Interface:

http://en.wikipedia.org/wiki/Marker_interface_pattern

mattcole
+1  A: 

Normally it's similar to attributes. Using attributes is a preferred to empty interfaces (at least as much as FxCop is aware). However .NET itself uses some of these interfaces like IRequiresSessionState and IReadOnlySessionState. I think there is performance loss in metadata lookup when you use attributes that made them use interfaces instead.

Mehrdad Afshari
A: 

They're called "Mark Interfaces" and are meant to signal instances of the marked classes.

For example... in C++ is a common practice to mark as "ICollectible" objects so they can be stored in generic non typed collections.

So like someone over says, they're to signal some object supported behavior, like ability to be collected, serialized, etc.

Juan Manuel
A: 

Empty interfaces are used to document that the classes that implement a given interface have a certain behaviour

For example in java the Cloneable interface in Java is an empty interface. When a class implements the Cloneable interface you know that you can call run the clone() on it.

hhafez
A: 

Been working with NServiceBus for the past year. While I wouldn't speak for Udi Dahan my understanding is that this interface is indeed used as a marker primarily.

Though I'd suggest you ask the man himself if he'd had thoughts of leaving this for future extension. My bet is no, as the mantra seems to be to keep messages very simple or at least practically platform agnostic.

Others answer well on the more general reasons for empty interfaces.

dove