/// <summary> /// Add an asynchronous message handler to this receiver /// </summary> /// <typeparam name="T">The type of message to receive</typeparam> /// <param name="receiveRegistration">The receive registration</param> /// <param name="onMessage">The message handler</param> /// <returns>'this' for fluent configuration</returns> public static IReceiveRegistration Add <T>(this IReceiveRegistration receiveRegistration, Action <T> onMessage) { Preconditions.CheckNotNull(receiveRegistration, "receiveRegistration"); var onMessageAsync = TaskHelpers.FromAction <T>((m, c) => onMessage(m)); return(receiveRegistration.Add(onMessageAsync)); }
// DO NOT DELETE. Discovered by reflection (see AddHandlers above) private void AddReceiveRegistration <T>(IReceiveRegistration receiveRegistration, ICommandHandler <T> commandHandler) where T : class { // receiveRegistration.Add ((Func<T, Task>)( // command => Task.Factory.StartNew (() => commandHandler.Handle (command)) // )); receiveRegistration.Add((Action <T>)commandHandler.Handle); }
public static IReceiveRegistration Add <TMessage>( this IReceiveRegistration receiveRegistration, IMediator mediator) where TMessage : class { if (mediator == null) { throw new ArgumentNullException(nameof(mediator)); } var handler = ConstructMessageHandler <TMessage>(mediator); return(receiveRegistration.Add <TMessage>(handler)); }
public void AddHandlers(IReceiveRegistration receiveRegistration) { // Find all types in this assembly that implement ICommandHandler<T> var handlerTypes = from t in this.GetType().Assembly.GetTypes() where t.GetInterfaces().Any(x => x.Name == typeof(ICommandHandler <>).Name) from i in t.GetInterfaces() let messageType = i.GetGenericArguments()[0] select new { HandlerType = t, MessageType = messageType, InterfaceType = typeof(ICommandHandler <>).MakeGenericType(messageType) }; // register all discovered handlers with the container foreach (var handlerType in handlerTypes) { container.Register(handlerType.InterfaceType, handlerType.HandlerType); logger.Log("Registered handler: {0} for message type {1}", handlerType.HandlerType.Name, handlerType.MessageType.Name); } // register handler instances to receive on the command queue var addMethod = this.GetType().GetMethod("AddReceiveRegistration", BindingFlags.NonPublic | BindingFlags.Instance); if (addMethod == null) { throw new ApplicationException("Couldn't resolve AddReceiveRegistration`1"); } foreach (var handlerType in handlerTypes) { var handler = container.Resolve(handlerType.InterfaceType); if (handler == null) { throw new ApplicationException(string.Format("Couldn't resolve handler: {0}", handlerType.InterfaceType.Name)); } addMethod.MakeGenericMethod(handlerType.MessageType).Invoke(this, new object[] { receiveRegistration, handler }); } }
public Task ResolveAsync(IReceiveRegistration registration) { registration.Add <T>(onMessage); return(Task.CompletedTask); }
protected override void AddHandlers(IReceiveRegistration handlers) => handlers.Add <CreatePriceForProduct>(DefaultHandleAsync);
protected abstract void AddHandlers(IReceiveRegistration handlers);
public void Register(IReceiveRegistration registration) { registration.Add <string>(Catch); }
/// <summary> /// Add an asynchronous message handler to this receiver /// </summary> /// <typeparam name="T">The type of message to receive</typeparam> /// <param name="receiveRegistration">The receive registration</param> /// <param name="onMessage">The message handler</param> /// <returns>'this' for fluent configuration</returns> public static IReceiveRegistration Add <T>(this IReceiveRegistration receiveRegistration, Func <T, Task> onMessage) { Preconditions.CheckNotNull(receiveRegistration, "receiveRegistration"); return(receiveRegistration.Add <T>((m, c) => onMessage(m))); }