public void Run() { this.parser.Parse(); string inputLine = this.inputOutputManager.ReadLine(); while (inputLine != TerminateProgramCommand) { string[] commandArgs = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (!Enum.TryParse(commandArgs[0], out RequestMethod requestMethod)) { throw new ArgumentException("Invalid request method type!"); } string commandParametersData = commandArgs[1]; foreach (var kvp in this.parser.Controllers[requestMethod]) { Regex requestMappingRegex = new Regex(kvp.Key); if (!requestMappingRegex.IsMatch(commandParametersData)) { continue; } ControllerActionPair controllerActionPair = kvp.Value; string[] targetMethodParameters = commandParametersData .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); object[] methodParameters = new object[controllerActionPair.ArgumentsMapping.Count]; int methodParameterIndex = 0; foreach (var indexTypePair in controllerActionPair.ArgumentsMapping) { methodParameters[methodParameterIndex] = Convert.ChangeType(targetMethodParameters[indexTypePair.Key], indexTypePair.Value); methodParameterIndex++; } string commandResult = controllerActionPair .Action .Invoke(controllerActionPair.Controller, methodParameters) .ToString(); this.inputOutputManager.WriteLine(commandResult); } inputLine = this.inputOutputManager.ReadLine(); } }
public void Parse() { Type[] allTypesInCurrentExecutingAssembly = Assembly.GetExecutingAssembly().GetTypes(); Type[] controllerTypes = allTypesInCurrentExecutingAssembly .Where(t => t.GetCustomAttributes <ControllerAttribute>().Any()) .ToArray(); foreach (Type controllerType in controllerTypes) { var currentTypeMethods = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (var currentMethod in currentTypeMethods) { var isCurrentMethodForRequestMapping = currentMethod .GetCustomAttributes() .Any(x => x.GetType() == typeof(RequestMappingAttribute)); if (isCurrentMethodForRequestMapping) { RequestMappingAttribute methodRequestMappingAttribute = currentMethod.GetCustomAttribute <RequestMappingAttribute>(); RequestMethod requestMethod = methodRequestMappingAttribute.Method; string mapping = methodRequestMappingAttribute.Value; List <string> mappingTokens = mapping.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList(); Dictionary <int, Type> argumentsMapping = new Dictionary <int, Type>(); for (int i = 0; i < mappingTokens.Count; i++) { var isTokenRequestParameter = mappingTokens[i].StartsWith("{") && mappingTokens[i].EndsWith("}"); if (isTokenRequestParameter) { foreach (ParameterInfo parameterInfo in currentMethod.GetParameters()) { int numberOfUriParameterAttributes = parameterInfo .GetCustomAttributes() .Count(x => x.GetType() == typeof(UriParameterAttribute)); if (numberOfUriParameterAttributes == 0) { continue; } UriParameterAttribute uriParameter = parameterInfo.GetCustomAttribute <UriParameterAttribute>(); bool isTargetMappingToken = mappingTokens[i].Equals("{" + uriParameter.Value + "}"); if (isTargetMappingToken) { argumentsMapping.Add(i, parameterInfo.ParameterType); string updatedMapping = mapping .Replace(mappingTokens[i].ToString(), parameterInfo.ParameterType == typeof(string) ? "\\w+" : "\\d+"); mapping = updatedMapping; break; } } } } Object controllerInstance = Activator.CreateInstance(controllerType); ControllerActionPair controllerActionPair = new ControllerActionPair(controllerInstance, currentMethod, argumentsMapping); if (!this.Controllers.ContainsKey(requestMethod)) { this.Controllers.Add(requestMethod, new Dictionary <string, ControllerActionPair>()); } this.Controllers[requestMethod].Add(mapping, controllerActionPair); } } } Type[] componentTypes = allTypesInCurrentExecutingAssembly .Where(t => t.GetCustomAttributes <ComponentAttribute>().Any()) .ToArray(); foreach (var componentType in componentTypes) { foreach (Type parent in componentType.GetInterfaces()) { this.Components.Add(parent, componentType); } } foreach (ControllerActionPair controllerActionPair in this.Controllers.Values.SelectMany(x => x.Values)) { this.ResolveDependencies(controllerActionPair.Controller); } }