private static IEnumerable<MethodCommandTypeTuple> GetSelfHandlerMethods(Type type, Type[] commandTypes, MethodParameterInjector injector) { if (!commandTypes.Contains(type)) yield break; var methods = type.GetMethods().Where(m => m.GetCustomAttribute<CommandHandlerAttribute>() != null); var enumerator = methods.GetEnumerator(); try { if (!enumerator.MoveNext()) yield break; else { var method = enumerator.Current; if (method.GetParameters().Any(p => !injector.CanSupply(p.ParameterType))) throw new CommandHandlerMethodHasUnsupportedParameter(type, method); yield return new MethodCommandTypeTuple { Method = method, CommandType = type, HandlerType = type}; } if (enumerator.MoveNext()) throw new CommandsMayOnlyDeclareOneHandlerMethod(type); } finally { enumerator.Dispose(); } }
public void Initialise(ConsoleApplicationBase app, MethodParameterInjector injector, CommandLineInterpreterConfiguration config, Dictionary<Type, ICommandHandler> handlers) { _app = app; _injector = injector; _config = config; _handlers = handlers; }
private static List<MethodCommandTypeTuple> GetHandlersForCommandTypes(Type type, Type[] commandTypes, MethodParameterInjector injector) { return type.GetMethods() .Select(m => new {Method = m, Params = m.GetParameters()}) .Where(p => commandTypes.Any(ct => IsValidCommandHandler(ct, p.Method, p.Params, injector))) .Select(p => new MethodCommandTypeTuple { Method = p.Method, HandlerType = p.Method.DeclaringType, CommandType = FindCommandTypeInMethodParameters(commandTypes, p.Method)}) .ToList(); }
private static List<MethodCommandTypeTuple> GetHandlersForSpecifiedType(Type type, CommandHandlerAttribute handlerAttribute, MethodParameterInjector injector) { return type.GetMethods() .Select(m => new {Method = m, Params = m.GetParameters()}) .Where(p => IsValidCommandHandler(handlerAttribute.CommandType, p.Method, p.Params, injector)) .Select(p => new MethodCommandTypeTuple{ Method = p.Method, HandlerType = p.Method.DeclaringType, CommandType = handlerAttribute.CommandType}) .ToList(); }
public void GetParameterReturnsAnInstanceMatchingTheTypeRequested() { var testImpl = new TestImpl(); var injector = new MethodParameterInjector(new object[] { "string", 5 }); injector.AddInstance<ITestInterface>(testImpl); Assert.That(injector.GetParameter<ITestInterface>(), Is.SameAs(testImpl)); }
public void CanSupplyASpecificInstanceForPredefinedParameterTypes() { var testImpl = new TestImpl(); var injector = new MethodParameterInjector(new object[] { "string", 5 }, new[] { new KeyValuePair<Type, object>(typeof(ITestInterface), testImpl) }); injector.GetParameters(_method3, new object[] { this }); Assert.That(injector.GetParameters(_method3, new object[] { this })[1], Is.SameAs(testImpl)); }
public InteractiveSession(ConsoleApplicationBase app, MethodParameterInjector injector, Dictionary<Type, ICommandHandler> handlers, IConsoleAdapter console, IErrorAdapter error, CommandLineInterpreterConfiguration config) { _app = app; _injector = injector; _handlers = handlers; _console = console; _error = error; _config = config; _interpreter = new CommandLineInterpreter(_config); }
internal void Execute(ConsoleApplicationBase app, object command, MethodParameterInjector injector) { try { var parameters = injector.GetParameters(_method, new[] { command }); MethodInvoker.Invoke(_method, app, parameters); } catch (Exception e) { Toolkit.HandleException(e, command, injector); } }
private void Execute(object command, MethodParameterInjector injector) { try { var parameters = injector.GetParameters(_method, new [] {command}); MethodInvoker.Invoke(_method, _handler, parameters); } catch (Exception e) { Toolkit.HandleException(e, command, injector); } }
public static IEnumerable<ICommandHandler> LoadHandlerMethods(Type type, Type[] commandTypes, MethodParameterInjector injector) { var selfHandlerMethods = GetSelfHandlerMethods(type, commandTypes, injector).ToList(); var externalHandlerMethods = GetHandlersForCommandTypes(type, commandTypes, injector); var handlerMethods = selfHandlerMethods.Concat(externalHandlerMethods) .GroupBy(g => g.CommandType).ToList(); var ambiguous = handlerMethods.FirstOrDefault(m => m.Count() > 1); if (ambiguous != null) { throw new MultipleHandlersForCommand(ambiguous.Key); } foreach (var handlerMethod in handlerMethods.SelectMany(h => h)) { yield return MakeHandlerMethod(handlerMethod); } }
public void Execute(ConsoleApplicationBase app, object command, IConsoleAdapter console, MethodParameterInjector injector, CommandExecutionMode executionMode) { var parameter = _parameterGetter == null || command == null ? String.Empty : _parameterGetter(command); if (String.IsNullOrWhiteSpace(parameter)) { CommandDescriber.Describe(_config, console, DefaultApplicationNameExtractor.Extract(app.GetType()), executionMode, Adorner); } else { var chosenCommand = _config.Commands.FirstOrDefault(c => String.CompareOrdinal(c.Name, parameter) == 0); if (chosenCommand == null) { console.WrapLine(@"The command ""{0}"" is not supported."); } else { CommandDescriber.Describe(chosenCommand, console, executionMode, Adorner); } } }
public static ICommandHandler Load(Type type, Type[] commandTypes, MethodParameterInjector injector) { var handlerAttribute = type.GetCustomAttribute<CommandHandlerAttribute>(); if (handlerAttribute == null) throw new CommandHandlerDoesNotHaveAttribute(type); var handlerMethods = handlerAttribute.CommandType == null ? GetHandlersForCommandTypes(type, commandTypes, injector) : GetHandlersForSpecifiedType(type, handlerAttribute, injector); if (handlerMethods.Count == 1) { var handlerMethod = handlerMethods[0]; return MakeHandler(handlerMethod.Method, handlerMethod.HandlerType, handlerMethod.CommandType); } if (handlerMethods.Count == 0) throw new NoCommandHandlerMethodFound(type); throw new AmbiguousCommandHandler(type); }
public void SpecificInstancesCanBeInjectedAfterConstruction() { var testImpl = new TestImpl(); var injector = new MethodParameterInjector(new object[] { "string", 5 }); injector.AddInstance<ITestInterface>(testImpl); injector.GetParameters(_method3, new object[] { this }); Assert.That(injector.GetParameters(_method3, new object[] { this })[1], Is.SameAs(testImpl)); }
public void CanSupplyReturnsFalseForTypeThatCannotBeProvided() { var injector = new MethodParameterInjector(new object[]{"string", 5}); Assert.That(injector.CanSupply(GetType()), Is.False); }
public void CanSupplyReturnsTrueForTypesWithSpecifiedInstances() { var injector = new MethodParameterInjector(new object[]{"string", 5}, new [] {new KeyValuePair<Type, object>(typeof(ITestInterface), new TestImpl()) }); Assert.That(injector.CanSupply(typeof(ITestInterface)), Is.True); }
public void CanSupplyReturnsTrueForTypeThatCanBeProvided() { var injector = new MethodParameterInjector(new object[]{"string", 5, this}); Assert.That(injector.CanSupply(GetType()), Is.True); }
public void SetUp() { _commandTypes = new[] {typeof (Command), typeof(Command2)}; //The handlers in the injector are just doubling up as parameter fodder - the specific types in the injector do not matter a jot. _injector = new MethodParameterInjector(new object[] { this, new Handler(), new Handler2(), new Handler3(), new Handler4(""), new Handler5(), new Handler6(), }); }
private static bool IsValidCommandHandler(Type commandType, MethodInfo method, ParameterInfo[] parameters, MethodParameterInjector injector) { if (method.DeclaringType != null && method.DeclaringType.GetProperties().Any(p => IsPropertyAccessor(method, p))) return false; return parameters.Any(p => p.ParameterType == commandType) && parameters.All(p => p.ParameterType == commandType || injector.CanSupply(p.ParameterType)); }
public void ConstructorProvidedInjectableIsUsed() { var injector = new MethodParameterInjector(new object[]{this}); Assert.That(injector.GetParameters(_method1, new object[] {}), Is.EqualTo(new object[] {this})); }
public void Execute(ConsoleApplicationBase app, object command, IConsoleAdapter console, MethodParameterInjector injector, CommandExecutionMode executionMode) { try { var parameters = injector.GetParameters(_method, new[] { command }); MethodInvoker.Invoke(_method, command, parameters); } catch (Exception e) { Toolkit.HandleException(e, command, injector); } }
public void Execute(ConsoleApplicationBase app, object command, IConsoleAdapter console, MethodParameterInjector injector, CommandExecutionMode executionMode) { Execute(app, command, injector); }
public void GetParameterReturnsNullForAnUnknownType() { var injector = new MethodParameterInjector(new object[] { "string", 5 }); Assert.That(injector.GetParameter<ITestInterface>(), Is.Null); }
public void AnExceptionIsThrownIfTheParametersCannotBeInjected() { var injector = new MethodParameterInjector(new object[]{5}); injector.GetParameters(_method2, new object[] {this}); }
public void MultipleParametersAreInjected() { var injector = new MethodParameterInjector(new object[]{5, "string"}); Assert.That(injector.GetParameters(_method2, new object[] {this}), Is.EqualTo(new object[] {this, 5, "string"})); }
public void GetParameterReturnsTheDefaultForAnUnknownValueType() { var injector = new MethodParameterInjector(new object[] { "string", 5 }); Assert.That(injector.GetParameter<DateTime>(), Is.EqualTo(default(DateTime))); }