Exemple #1
0
        /// <inheritdoc/>
        /// <remarks>Commands will be loaded regardless of presence of <see cref="CommandsHandlerAttribute"/>.</remarks>
        /// <exception cref="InvalidOperationException">No mapped initializer for a command was found.</exception>
        public Task <IEnumerable <ICommandInstanceDescriptor> > LoadFromMethodAsync(MethodInfo method, CancellationToken cancellationToken = default)
        {
            List <ICommandInstanceDescriptor> results = new List <ICommandInstanceDescriptor>();

            _log?.LogTrace("Loading command {Name}", method.Name);
            IEnumerable <CommandAttributeBase> attributes = method.GetCustomAttributes <CommandAttributeBase>(true);

            if (!attributes.Any())
            {
                _log?.LogWarning("Cannot initialize command from {Handler}'s method {Name} - {Attribute} missing", method.DeclaringType.FullName, method.Name, nameof(CommandAttributeBase));
                return(NullTask());
            }
            foreach (CommandAttributeBase attribute in attributes)
            {
                // ensure there's a valid initializer
                ICommandInitializer initializer = _initializers.GetInitializer(attribute.GetType());
                if (initializer == null)
                {
                    throw new InvalidOperationException($"No initializer found for command type {attribute.GetType().Name}");
                }

                // add the command
                CommandInstanceDescriptor descriptor = new CommandInstanceDescriptor(attribute, method);
                results.Add(new CommandInstanceDescriptor(attribute, method));
                _log?.LogTrace("Command {Name} from handler {Handler} loaded", descriptor.Method.Name, descriptor.GetHandlerType().Name);
            }
            return(Task.FromResult <IEnumerable <ICommandInstanceDescriptor> >(results));
        }
 /// <summary>Gets a command initializer for the command type.</summary>
 /// <typeparam name="T">Type of the command.</typeparam>
 /// <returns>An initializer for provided command type.</returns>
 public static ICommandInitializer GetMappedInitializer <T>(this ICommandInitializerProvider map) where T : CommandAttributeBase
 => map.GetInitializer(typeof(T));