InterfaceEvents 1.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package InterfaceEvents --version 1.5.0                
NuGet\Install-Package InterfaceEvents -Version 1.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InterfaceEvents" Version="1.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InterfaceEvents --version 1.5.0                
#r "nuget: InterfaceEvents, 1.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InterfaceEvents as a Cake Addin
#addin nuget:?package=InterfaceEvents&version=1.5.0

// Install InterfaceEvents as a Cake Tool
#tool nuget:?package=InterfaceEvents&version=1.5.0                

Features

  • Items with a higher priority are called first. negative priorities are fine.
  • Subscribing/Unsubscribing during an event will take effect afterwards.
  • Optionally only notify subscribers once. After the initial event, any new subscribers are notified apon subscription.
  • Optionally only notify subscribers with the highest priority.
  • Optionally Create Priority Dictionaries that determine what order types should receive events in relative to eachother.

Why interfaces?

Pros

  • No unesseceary Sender/Args parameters (though you can add them if needed)
  • more explicit declerations:
    • It's immedately clear when a class receives events
    • Guarenteed to use the same method names
  • personally prefer ".Subscribe(x)" to "+="
  • The above features I've been able to add

Cons

  • Not core to c#
  • Sending an event has slighty weird syntax:
    • myEvent.Send((IOnMyEvent subscriber) => subscriber.OnMyEvent());
    • This is to allow any method signature to be used for receving the event.

Examples / Documentation

Getting Started

Creating and subscribing to event

using DudCo.Events;

interface IOnEvent
{
    void OnEvent();
}

class EventReceiver : IOnEvent
{
    public void OnEvent()
    {
        //do event stuff
    }
}

class EventsExample
{
    public void Main()
    {
         EventSender<IOnEvent> someEvent = new EventSender<IOnEvent>();
         EventReceiver receiver = new EventReceiver()
         someEvent.Subscribe(reveiver);
         someEvent.Send((IOnEvent subscriber) => subscriber.OnEvent());
    }
}

Items can only be subscribed to an event once, and you'll get a System.ArgumentException if you try to subscribe multiple times.

<hr>

Subscribing with priority

someEvent.Subscribe(something, 5)

someEvent.Subscribe(somethingElse, -2)

<hr>

Subscribing in bulk

Event.Subscribe(a, b);

when bulk subscribing, the priority must be specified first:

Event.Subscribe(2, c, d, e);

also works with unsubscribing:

Event.Unsubscribe(a, b, c, d, e);

<hr>

TrySubscribe

If the item you're trying to subscribe might allready be subscribed, you can use TrySubscribe() and TryUnsubscribe() methods. They will not throw exceptions, but instead return a sucsess/failure bool.

Dont use it everywhere, only if you need to, as it will hide bugs (by not throwing).

<hr>

Sending event with parameters

public interface IOnCollision
{
    void OnCollision(CollisionInfo collision);
}

private void SendCollisionEvent(CollisionInfo info)
{
    void action(IOnCollision subscriber) => subscriber.OnCollision(info);
    collisionEvent.Send(action);
}

<hr>

Optional Features

EventBuilder

The event builder can be used to create EventSenders with advanced features.

This will create the default EventSender, same as using new EventSender():

EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
            .Build();

<hr>

Type Priority Dictionaries

Type priority dictionaries let you specify what priority all instances of a type will be given when subscribing via eventSender.SubscribeByRegisteredType()

An item in the dictionary subscribed with eventSender.Subscribe() will throw an exception.

public class MyPriorityDictionary : PriorityDictionary
    {
        public MyPriorityDictionary()
        {
            Add<ClassA>(0);
            Add<ClassB>(After<ClassA>());
            Add<ClassC>(Before<ClassB>());
        }
    }

Create by either passing in the constructor, or using an EventBuilder.

EventSender<IOnEvent> myEvent = new EventSender<IOnEvent>(new MyPriorityDictionary());
EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
            .WithPriorityDictionary(new MyPriorityDictionary())
            .Build();

<hr>

Only notify highest priority

This option will only send events to the subscriber with the highest priortiy. all subscribers which share the highest priority are notified.

EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
            .SendOnlyHighestPriority()
            .Build();

<hr>

Only notify once

This option will only alow an event to be sent once. after which:

  • current subscribers are unsubscribed
  • new subscribers are not subscribed, but instead receive the event immediately

this means that the EventSender holds no references to the subscribers after the event has been sent, so they may be garbage collected.

this option is usefull for delaying the creation of dependant objects untill thier dependancies have been created.

trying to send the event multiple times will throw a System.InvalidOperationException.

EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
            .SendOnlyOnce()
            .Build();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 395 11/12/2022
2.0.0 398 10/16/2022
1.5.0 435 4/21/2022