What is an empty interface used for

后端 未结 12 2721
醉话见心
醉话见心 2020-12-13 17:18

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the us

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 18:12

    An empty interface can be used to classify classes under a specific purpose. (Marker Interface)

    Example : Database Entities

    public interface IEntity {
    
    }
    
    public class Question implements IEntity {
       // Implementation Goes Here
    }
    
    public class Answer implements IEntity {
       // Implementation Goes Here
    }
    

    For Instance, If you will be using Generic Repository(ex. IEntityRepository), using generic constraints, you can prevent the classes that do not implement the IEntity interface from being sent by the developers.

提交回复
热议问题