From 08d80404ec232907a1ab5ed48c8347d0eacd1d0f Mon Sep 17 00:00:00 2001 From: Antony Male Date: Mon, 24 Feb 2014 08:06:56 +0000 Subject: [PATCH] WIP on the EventAggregator --- Stylet/EventAggregator.cs | 67 ++++++++++++++++++++++++++ Stylet/Stylet.csproj | 1 + Stylet/StyletIoC/StyletIoCContainer.cs | 10 ++-- 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 Stylet/EventAggregator.cs diff --git a/Stylet/EventAggregator.cs b/Stylet/EventAggregator.cs new file mode 100644 index 0000000..42c7eff --- /dev/null +++ b/Stylet/EventAggregator.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Stylet +{ + public interface IHandle + { + } + + public interface IHandle : IHandle + { + void Handle(TMessageType message); + } + + public class EventAggregator + { + private readonly List handlers = new List(); + + public void Subscribe(IHandle handler) + { + + } + + public void Publish(object message) + { + + } + + private class Handler + { + private readonly WeakReference target; + private readonly Dictionary> handlers = new Dictionary>(); + + public Handler(object handler) + { + this.target = new WeakReference(target); + + foreach (var implementation in this.target.GetType().GetInterfaces().Where(x => x.IsGenericType && typeof(IHandle).IsAssignableFrom(x))) + { + var type = implementation.GetGenericArguments()[0]; + var method = type.GetMethod("Handle"); + var param = Expression.Parameter(type, "message"); + var caller = Expression.Lambda>(Expression.Call(method, param), param).Compile(); + this.handlers.Add(type, caller); + } + } + + public bool IsOfType(object subscriberType) + { + return this.target.Target == subscriberType; + } + + public bool Handle(Type messageType, object message) + { + var target = this.target.Target; + if (target == null) + return false; + + return true; + } + } + } +} diff --git a/Stylet/Stylet.csproj b/Stylet/Stylet.csproj index ec3eebd..6a400f5 100644 --- a/Stylet/Stylet.csproj +++ b/Stylet/Stylet.csproj @@ -53,6 +53,7 @@ + diff --git a/Stylet/StyletIoC/StyletIoCContainer.cs b/Stylet/StyletIoC/StyletIoCContainer.cs index a9d9623..58caa8a 100644 --- a/Stylet/StyletIoC/StyletIoCContainer.cs +++ b/Stylet/StyletIoC/StyletIoCContainer.cs @@ -88,25 +88,25 @@ namespace StyletIoC /// /// Maps a [type, key] pair to a collection of registrations for that keypair. You can retrieve an instance of the type from the registration /// - private ConcurrentDictionary registrations = new ConcurrentDictionary(); + private readonly ConcurrentDictionary registrations = new ConcurrentDictionary(); /// /// Maps a [type, key] pair, where 'type' is the T in IEnumerable{T}, to a registration which can create a List{T} implementing that IEnumerable. /// This is separate from 'registrations' as some code paths - e.g. Get() - won't search it (while things like constructor/property injection will). /// - private ConcurrentDictionary getAllRegistrations = new ConcurrentDictionary(); + private readonly ConcurrentDictionary getAllRegistrations = new ConcurrentDictionary(); /// /// Maps a [type, key] pair, where 'type' is an unbound generic (something like IValidator{}) to something which, given a type, can create an IRegistration for that type. /// So if they've bound an IValidator{} to a an IntValidator, StringValidator, etc, and request an IValidator{string}, one of the UnboundGenerics here can generatr a StringValidator. /// Type-safety with the List is ensured by locking using the list as the lock object before modifying / iterating. /// - private ConcurrentDictionary> unboundGenerics = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> unboundGenerics = new ConcurrentDictionary>(); /// /// Maps a type onto a BuilderUpper for that type, which can create an Expresson/Delegate to build up that type. /// - private ConcurrentDictionary builderUppers = new ConcurrentDictionary(); + private readonly ConcurrentDictionary builderUppers = new ConcurrentDictionary(); /// /// Cached ModuleBuilder used for building factory implementations @@ -116,7 +116,7 @@ namespace StyletIoC /// /// Cache of services registered with .ToAbstractFactory() to the factory which was generated for each. /// - private ConcurrentDictionary factories = new ConcurrentDictionary(); + private readonly ConcurrentDictionary factories = new ConcurrentDictionary(); /// /// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency