Commit dd4d055
Changed files (4)
trunk
product
MyMoney
Infrastructure
Utility
Extensions
trunk/product/MyMoney/Infrastructure/eventing/EventAggregator.cs
@@ -1,5 +1,7 @@
+using System;
using System.Collections.Generic;
using System.Linq;
+using System.Threading;
using MoMoney.Utility.Extensions;
namespace MoMoney.Infrastructure.eventing
@@ -7,32 +9,42 @@ namespace MoMoney.Infrastructure.eventing
public interface IEventAggregator
{
void subscribe_to<Event>(IEventSubscriber<Event> subscriber) where Event : IEvent;
+ void subscribe(object subscriber);
void publish<Event>(Event the_event_to_broadcast) where Event : IEvent;
void publish<Event>() where Event : IEvent, new();
}
public class EventAggregator : IEventAggregator
{
- readonly IDictionary<string, List<object>> subscribers;
+ //readonly IDictionary<string, List<object>> subscribers;
+ readonly SynchronizationContext context;
+ readonly HashSet<object> listeners;
+ readonly object mutex;
public EventAggregator()
{
- subscribers = new Dictionary<string, List<object>>();
+ //subscribers = new Dictionary<string, List<object>>();
+ listeners = new HashSet<object>();
+ mutex = new object();
}
public void subscribe_to<Event>(IEventSubscriber<Event> subscriber) where Event : IEvent
{
- if (!get_list_for<Event>().Contains(subscriber))
- {
- get_list_for<Event>().Add(subscriber);
- }
+ subscribe(subscriber);
+ }
+
+ public void subscribe(object subscriber)
+ {
+ within_lock(() => listeners.Add(subscriber));
}
public void publish<Event>(Event the_event_to_broadcast) where Event : IEvent
{
- get_list_for<Event>()
- .Select(x => x.downcast_to<IEventSubscriber<Event>>())
- .each(x => x.notify(the_event_to_broadcast));
+ //get_list_for<Event>()
+ // .Select(x => x.downcast_to<IEventSubscriber<Event>>())
+ // .each(x => x.notify(the_event_to_broadcast));
+
+ listeners.call_on_each<IEventSubscriber<Event>>(x => x.notify(the_event_to_broadcast));
}
public void publish<Event>() where Event : IEvent, new()
@@ -40,13 +52,21 @@ namespace MoMoney.Infrastructure.eventing
publish(new Event());
}
- List<object> get_list_for<Event>()
+ //List<object> get_list_for<Event>()
+ //{
+ // if (!subscribers.ContainsKey(typeof (Event).FullName))
+ // {
+ // subscribers.Add(typeof (Event).FullName, new List<object>());
+ // }
+ // return subscribers[typeof (Event).FullName];
+ //}
+
+ void within_lock(Action action)
{
- if (!subscribers.ContainsKey(typeof (Event).FullName))
+ lock (mutex)
{
- subscribers.Add(typeof (Event).FullName, new List<object>());
+ action();
}
- return subscribers[typeof (Event).FullName];
}
}
}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/eventing/EventAggregatorSpecs.cs
@@ -31,7 +31,7 @@ namespace MoMoney.Infrastructure.eventing
because b = () =>
{
sut.subscribe_to(first_subscriber);
- sut.subscribe_to(second_subscriber);
+ sut.subscribe(second_subscriber);
sut.publish(message);
};
trunk/product/MyMoney/Utility/Extensions/conversion_extensions.cs → trunk/product/MyMoney/Utility/Extensions/ConversionExtensions.cs
@@ -1,8 +1,9 @@
using System;
+using System.Collections;
namespace MoMoney.Utility.Extensions
{
- public static class conversion_extensions
+ public static class ConversionExtensions
{
public static T downcast_to<T>(this object object_to_cast)
{
@@ -11,7 +12,8 @@ namespace MoMoney.Utility.Extensions
public static T converted_to<T>(this object item_to_convert)
{
- if (item_to_convert.is_an_implementation_of<IConvertible>()) {
+ if (item_to_convert.is_an_implementation_of<IConvertible>())
+ {
return (T) Convert.ChangeType(item_to_convert, typeof (T));
}
return item_to_convert.downcast_to<T>();
@@ -21,5 +23,21 @@ namespace MoMoney.Utility.Extensions
{
return item is T;
}
+
+ public static void call_on<T>(this object target, Action<T> action) where T : class
+ {
+ if (target as T != null)
+ {
+ action(target as T);
+ }
+ }
+
+ public static void call_on_each<T>(this IEnumerable items, Action<T> action) where T : class
+ {
+ foreach (var item in items)
+ {
+ item.call_on(action);
+ }
+ }
}
}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -621,7 +621,7 @@
<Compile Include="Utility\Core\Map.cs" />
<Compile Include="Utility\Core\OrSpecification.cs" />
<Compile Include="Utility\Extensions\configuration_extensions.cs" />
- <Compile Include="Utility\Extensions\conversion_extensions.cs" />
+ <Compile Include="Utility\Extensions\ConversionExtensions.cs" />
<Compile Include="Utility\Extensions\EnumerableExtensions.cs" />
<Compile Include="Utility\Extensions\func_extensions.cs" />
<Compile Include="Utility\Extensions\mapping_extensions.cs" />