WIP on the EventAggregator

This commit is contained in:
Antony Male 2014-02-24 08:06:56 +00:00
parent b13908e762
commit 08d80404ec
3 changed files with 73 additions and 5 deletions

67
Stylet/EventAggregator.cs Normal file
View File

@ -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<TMessageType> : IHandle
{
void Handle(TMessageType message);
}
public class EventAggregator
{
private readonly List<Handler> handlers = new List<Handler>();
public void Subscribe(IHandle handler)
{
}
public void Publish(object message)
{
}
private class Handler
{
private readonly WeakReference target;
private readonly Dictionary<Type, Action<object>> handlers = new Dictionary<Type, Action<object>>();
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<Action<object>>(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;
}
}
}
}

View File

@ -53,6 +53,7 @@
<Compile Include="ConductorBase.cs" />
<Compile Include="ConductorBaseWithActiveItem.cs" />
<Compile Include="ConductorOneActive.cs" />
<Compile Include="EventAggregator.cs" />
<Compile Include="EventCommand.cs" />
<Compile Include="Execute.cs" />
<Compile Include="IConductor.cs" />

View File

@ -88,25 +88,25 @@ namespace StyletIoC
/// <summary>
/// Maps a [type, key] pair to a collection of registrations for that keypair. You can retrieve an instance of the type from the registration
/// </summary>
private ConcurrentDictionary<TypeKey, IRegistrationCollection> registrations = new ConcurrentDictionary<TypeKey, IRegistrationCollection>();
private readonly ConcurrentDictionary<TypeKey, IRegistrationCollection> registrations = new ConcurrentDictionary<TypeKey, IRegistrationCollection>();
/// <summary>
/// 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).
/// </summary>
private ConcurrentDictionary<TypeKey, IRegistration> getAllRegistrations = new ConcurrentDictionary<TypeKey, IRegistration>();
private readonly ConcurrentDictionary<TypeKey, IRegistration> getAllRegistrations = new ConcurrentDictionary<TypeKey, IRegistration>();
/// <summary>
/// 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.
/// </summary>
private ConcurrentDictionary<TypeKey, List<UnboundGeneric>> unboundGenerics = new ConcurrentDictionary<TypeKey, List<UnboundGeneric>>();
private readonly ConcurrentDictionary<TypeKey, List<UnboundGeneric>> unboundGenerics = new ConcurrentDictionary<TypeKey, List<UnboundGeneric>>();
/// <summary>
/// Maps a type onto a BuilderUpper for that type, which can create an Expresson/Delegate to build up that type.
/// </summary>
private ConcurrentDictionary<Type, BuilderUpper> builderUppers = new ConcurrentDictionary<Type, BuilderUpper>();
private readonly ConcurrentDictionary<Type, BuilderUpper> builderUppers = new ConcurrentDictionary<Type, BuilderUpper>();
/// <summary>
/// Cached ModuleBuilder used for building factory implementations
@ -116,7 +116,7 @@ namespace StyletIoC
/// <summary>
/// Cache of services registered with .ToAbstractFactory() to the factory which was generated for each.
/// </summary>
private ConcurrentDictionary<Type, Type> factories = new ConcurrentDictionary<Type, Type>();
private readonly ConcurrentDictionary<Type, Type> factories = new ConcurrentDictionary<Type, Type>();
/// <summary>
/// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency