接口继承模式
在 eShop 示例项目中看到一个很棒的接口设计,这里直接把代码贴出来了:
1
2
3
4
5
6
7
8
9
10
11
12
public interface IIntegrationEventHandler<in TIntegrationEvent> : IIntegrationEventHandler
where TIntegrationEvent : IntegrationEvent
{
Task Handle(TIntegrationEvent @event);
Task IIntegrationEventHandler.Handle(IntegrationEvent @event) => Handle((TIntegrationEvent)@event);
}
public interface IIntegrationEventHandler
{
Task Handle(IntegrationEvent @event);
}
当需要用一个集合来管理泛型类型的不同实现时,这种模式尤为奏效:
这里就是,因为事件系统需要一个 List<EventHandler<???>>
来管理所有事件处理器,所以采用了这种接口继承模式。
This post is licensed under CC BY 4.0 by the author.