public interface IDependency { event EventHandler ItHappened; }public class NeedyClass { private readonly IDependency dependency; public NeedyClass(IDependency dependency) { if (dependency == null) throw new ArgumentNullException("dependency"); this.dependency = dependency; this.dependency.ItHappened += this.OnItHappened; } private void OnItHappened(object sender, EventArgs e) { // Handle event here } }
public class NeedyClass { public void DoSomethingInteresting() { // Handle event here } }// Third party: var nc = new NeedyClass(); dependency.ItHappened += (s, e) => nc.DoSomethingInteresting();
public interface IDependency : IObservable{ } public class NeedyClass : IObserver , IDisposable { private readonly IDisposable subscription; public NeedyClass(IObservable dependency) { if (dependency == null) throw new ArgumentNullException("dependency"); this.subscription = dependency.Subscribe(this); } public void OnCompleted() { } public void OnError(Exception error) { } public void OnNext(Unit value) { // Handle event here } public void Dispose() { this.subscription.Dispose(); } }