Raising events vs direct method calls differences

前端 未结 6 2050
夕颜
夕颜 2021-02-04 11:27

Raising an event, will call its event handler. eg http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

What is the difference between using the events mechan

6条回答
  •  盖世英雄少女心
    2021-02-04 12:00

    In addition to the multiple/no subscribers scenarios above, events are also used to reduce code coupling - eg method A() doesn't need to know anything about about method B() at compile time. This enables better separation of concerns and less fragile code.

    In the wild, you're more likely to see events used in framework and UI code, whereas within the domain logic of an application developers more often use things like Separated Interface and Dependency Injection to decouple code. Recently there has been a bit more discussion in various arenas regarding using events within domain logic, an approach which is cunningly named Domain Events.

提交回复
热议问题