summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo k <mo@mokhan.ca>2012-05-30 21:48:45 -0600
committermo k <mo@mokhan.ca>2012-05-30 21:48:45 -0600
commitfb697a4189ca283ed57bbf05cd8ac503fc87dd78 (patch)
tree56cabadeecb1039b957c0fb89390202a8775259e
parentbc25ab8998da34af01aff98aea03de1a8d412792 (diff)
start to build a simple event aggregator.
-rwxr-xr-xsrc/core/Casting.cs10
-rwxr-xr-xsrc/core/Dynamic.cs12
-rwxr-xr-xsrc/core/IEventAggregator.cs4
-rwxr-xr-xsrc/core/IEventRegistry.cs7
-rwxr-xr-xsrc/core/IPublishEvents.cs7
-rwxr-xr-xsrc/core/ISubscribeTo.cs7
-rwxr-xr-xsrc/core/Iterating.cs14
-rwxr-xr-xsrc/core/ThreadUnsafeEventAggregator.cs19
-rwxr-xr-xsrc/core/core.csproj8
-rwxr-xr-xsrc/specs/EventAggregatorSpec.cs36
-rwxr-xr-xsrc/specs/specs.csproj1
11 files changed, 125 insertions, 0 deletions
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<T>(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<DynamicType>(this object target, Action<DynamicType> command) where DynamicType : class
+ {
+ command(target.DowncastTo<DynamicType>());
+ }
+ }
+} \ 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<Event>(ISubscribeTo<Event> 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>(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<Event>
+ {
+ 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<T>(this IEnumerable<T> items, Action<T> visitor)
+ {
+ foreach (var item in items ?? Enumerable.Empty<T>()) 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<object> subscribers = new List<object>();
+
+ public void RegisterFor<Event>(ISubscribeTo<Event> subscriber)
+ {
+ subscribers.Add(subscriber);
+ }
+
+ public void Publish<Event>(Event theEvent)
+ {
+ subscribers.Each(x => x.CallAs<ISubscribeTo<Event>>(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 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Casting.cs" />
+ <Compile Include="Dynamic.cs" />
+ <Compile Include="IEventAggregator.cs" />
+ <Compile Include="IEventRegistry.cs" />
+ <Compile Include="IPublishEvents.cs" />
+ <Compile Include="ISubscribeTo.cs" />
+ <Compile Include="Iterating.cs" />
<Compile Include="RoboMom.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="ThreadUnsafeEventAggregator.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
diff --git a/src/specs/EventAggregatorSpec.cs b/src/specs/EventAggregatorSpec.cs
new file mode 100755
index 0000000..d40b9f8
--- /dev/null
+++ b/src/specs/EventAggregatorSpec.cs
@@ -0,0 +1,36 @@
+using Machine.Specifications;
+using Rhino.Mocks;
+
+namespace specs
+{
+ public class EventAggregatorSpec
+ {
+ Establish context = () =>
+ {
+ sut = new ThreadUnsafeEventAggregator();
+ };
+
+ static IEventAggregator sut;
+
+ public class when_publishing
+ {
+ It should_notify_all_interested_subscribers = () =>
+ {
+ handler.AssertWasCalled(x => x.Notify("hello"));
+ };
+
+ Establish context = () =>
+ {
+ handler = MockRepository.GenerateMock<ISubscribeTo<string>>();
+ };
+
+ Because of = () =>
+ {
+ sut.RegisterFor(handler);
+ sut.Publish("hello");
+ };
+
+ static ISubscribeTo<string> handler;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/specs/specs.csproj b/src/specs/specs.csproj
index afcb377..d0ecf7c 100755
--- a/src/specs/specs.csproj
+++ b/src/specs/specs.csproj
@@ -52,6 +52,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="EventAggregatorSpec.cs" />
<Compile Include="RoboMomSpecs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>