From fb697a4189ca283ed57bbf05cd8ac503fc87dd78 Mon Sep 17 00:00:00 2001 From: mo k Date: Wed, 30 May 2012 21:48:45 -0600 Subject: start to build a simple event aggregator. --- src/core/Casting.cs | 10 +++++++++ src/core/Dynamic.cs | 12 +++++++++++ src/core/IEventAggregator.cs | 4 ++++ src/core/IEventRegistry.cs | 7 +++++++ src/core/IPublishEvents.cs | 7 +++++++ src/core/ISubscribeTo.cs | 7 +++++++ src/core/Iterating.cs | 14 +++++++++++++ src/core/ThreadUnsafeEventAggregator.cs | 19 +++++++++++++++++ src/core/core.csproj | 8 ++++++++ src/specs/EventAggregatorSpec.cs | 36 +++++++++++++++++++++++++++++++++ src/specs/specs.csproj | 1 + 11 files changed, 125 insertions(+) create mode 100755 src/core/Casting.cs create mode 100755 src/core/Dynamic.cs create mode 100755 src/core/IEventAggregator.cs create mode 100755 src/core/IEventRegistry.cs create mode 100755 src/core/IPublishEvents.cs create mode 100755 src/core/ISubscribeTo.cs create mode 100755 src/core/Iterating.cs create mode 100755 src/core/ThreadUnsafeEventAggregator.cs create mode 100755 src/specs/EventAggregatorSpec.cs diff --git a/src/core/Casting.cs b/src/core/Casting.cs new file mode 100755 index 0000000..dbc25f0 --- /dev/null +++ b/src/core/Casting.cs @@ -0,0 +1,10 @@ +namespace core +{ + public static class Casting + { + public static T DowncastTo(this object item) where T : class + { + return item as T; + } + } +} \ No newline at end of file diff --git a/src/core/Dynamic.cs b/src/core/Dynamic.cs new file mode 100755 index 0000000..ae4a627 --- /dev/null +++ b/src/core/Dynamic.cs @@ -0,0 +1,12 @@ +using System; + +namespace core +{ + public static class Dynamic + { + public static void CallAs(this object target, Action command) where DynamicType : class + { + command(target.DowncastTo()); + } + } +} \ No newline at end of file diff --git a/src/core/IEventAggregator.cs b/src/core/IEventAggregator.cs new file mode 100755 index 0000000..91758f7 --- /dev/null +++ b/src/core/IEventAggregator.cs @@ -0,0 +1,4 @@ +namespace core +{ + public interface IEventAggregator : IEventRegistry, IPublishEvents {} +} \ No newline at end of file diff --git a/src/core/IEventRegistry.cs b/src/core/IEventRegistry.cs new file mode 100755 index 0000000..db9c475 --- /dev/null +++ b/src/core/IEventRegistry.cs @@ -0,0 +1,7 @@ +namespace core +{ + public interface IEventRegistry + { + void RegisterFor(ISubscribeTo subscriber); + } +} \ No newline at end of file diff --git a/src/core/IPublishEvents.cs b/src/core/IPublishEvents.cs new file mode 100755 index 0000000..af540c3 --- /dev/null +++ b/src/core/IPublishEvents.cs @@ -0,0 +1,7 @@ +namespace core +{ + public interface IPublishEvents + { + void Publish(Event theEvent); + } +} \ No newline at end of file diff --git a/src/core/ISubscribeTo.cs b/src/core/ISubscribeTo.cs new file mode 100755 index 0000000..b2fd12e --- /dev/null +++ b/src/core/ISubscribeTo.cs @@ -0,0 +1,7 @@ +namespace core +{ + public interface ISubscribeTo + { + void Notify(Event subscribedEvent); + } +} \ No newline at end of file diff --git a/src/core/Iterating.cs b/src/core/Iterating.cs new file mode 100755 index 0000000..3e7de14 --- /dev/null +++ b/src/core/Iterating.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace core +{ + public static class Iterating + { + public static void Each(this IEnumerable items, Action visitor) + { + foreach (var item in items ?? Enumerable.Empty()) visitor(item); + } + } +} \ No newline at end of file diff --git a/src/core/ThreadUnsafeEventAggregator.cs b/src/core/ThreadUnsafeEventAggregator.cs new file mode 100755 index 0000000..c0d902f --- /dev/null +++ b/src/core/ThreadUnsafeEventAggregator.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace core +{ + public class ThreadUnsafeEventAggregator : IEventAggregator + { + List subscribers = new List(); + + public void RegisterFor(ISubscribeTo subscriber) + { + subscribers.Add(subscriber); + } + + public void Publish(Event theEvent) + { + subscribers.Each(x => x.CallAs>(y => y.Notify(theEvent))); + } + } +} \ No newline at end of file diff --git a/src/core/core.csproj b/src/core/core.csproj index 0808656..2f4d1cf 100755 --- a/src/core/core.csproj +++ b/src/core/core.csproj @@ -43,8 +43,16 @@ + + + + + + + +