コード例 #1
0
        public string Dispatch(RequestMethod requestMethod, string uri)
        {
            IDictionary <string, ControllerActionPair> innerDictionary = controllers[requestMethod];

            foreach (var parameters in innerDictionary.Keys)
            {
                Regex regex = new Regex(parameters);
                if (regex.IsMatch(uri))
                {
                    ControllerActionPair pair = innerDictionary[parameters];
                    object     controller     = pair.Controller;
                    MethodInfo methodToInvoke = pair.Action;
                    IDictionary <int, Type> argumentsByPosition = pair.ArgumentsMapping;

                    string[] uriTokens = uri.Split('/');

                    object[] argumentsToPass = new object[argumentsByPosition.Count];
                    int      index           = 0;
                    foreach (var typeMapping in argumentsByPosition)
                    {
                        string valueToParse    = uriTokens[typeMapping.Key];
                        Type   typeToParseFrom = typeMapping.Value;
                        argumentsToPass[index++] = typeConversions[typeToParseFrom].Invoke(valueToParse);
                    }

                    var result = methodToInvoke.Invoke(controller, argumentsToPass);
                    return(result.ToString());
                }
            }

            return(null);
        }
コード例 #2
0
        public string Dispatch(RequestMethod requestMethod, string uri)
        {
            string[] uriTokens = uri.Split('/');

            IDictionary <string, ControllerActionPair> innerDictionary
                = this.controllers[requestMethod];

            foreach (var regexControllerPair in innerDictionary)
            {
                string regex = regexControllerPair.Key;
                ControllerActionPair controllerAction = regexControllerPair.Value;
                var      argumentsMapping             = controllerAction.ArgumentsMapping;
                int      index           = 0;
                object[] argumentsToPass = new object[argumentsMapping.Count];
                if (Regex.IsMatch(uri, regex))
                {
                    foreach (var positionTypesPair in argumentsMapping)
                    {
                        string singleArgument = uriTokens[positionTypesPair.Key];
                        Type   typeToCast     = positionTypesPair.Value;
                        object argumentToPass = Convert.ChangeType(singleArgument, typeToCast);
                        argumentsToPass[index++] = argumentToPass;
                    }

                    object     controller = controllerAction.Controller;
                    MethodInfo method     = controllerAction.Action;

                    return((string)method.Invoke(controller, argumentsToPass));
                }
            }

            return(null);
        }
コード例 #3
0
 private void ResolveControllerDependency(ControllerActionPair pair)
 {
     try
     {
         this.container.ResolveDependencies(pair.Controller);
     }
     catch (Exception e)
     {
         throw new InvalidOperationException(e.Message);
     }
 }
コード例 #4
0
        public void Parse()
        {
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (Type type in types)
            {
                if (type.GetCustomAttributes().Any(x => x.GetType() == typeof(ControllerAttribute)))
                {
                    foreach (var currentMethod in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
                    {
                        if (currentMethod.GetCustomAttributes().Any(x => x.GetType() == typeof(RequestMappingAttribute)))
                        {
                            RequestMappingAttribute requestMapping = currentMethod.GetCustomAttribute <RequestMappingAttribute>();
                            RequestMethod           requestMethod  = requestMapping.Method;
                            string        mapping       = requestMapping.Value;
                            List <string> mappingTokens = mapping.Split('/').ToList();

                            Dictionary <int, Type> argumentsMapping = new Dictionary <int, Type>();

                            for (int i = 0; i < mappingTokens.Count; i++)
                            {
                                if (mappingTokens[i].StartsWith("{") && mappingTokens[i].EndsWith("}"))
                                {
                                    foreach (ParameterInfo parameterInfo in currentMethod.GetParameters())
                                    {
                                        if (parameterInfo.GetCustomAttributes().All(x => x.GetType() != typeof(UriParameterAttribute)))
                                        {
                                            continue;
                                        }

                                        UriParameterAttribute uriParameter =
                                            parameterInfo.GetCustomAttribute <UriParameterAttribute>();
                                        if (mappingTokens[i].Equals("{" + uriParameter.Value + "}"))
                                        {
                                            argumentsMapping.Add(i, parameterInfo.ParameterType);


                                            mapping = mapping.Replace(mappingTokens[i].ToString(), parameterInfo.GetType() == typeof(string) ? "\\w+" : "\\d+");
                                            break;
                                        }
                                    }
                                }
                            }

                            Object controllerInstance = Activator.CreateInstance(type);

                            ControllerActionPair pair = new ControllerActionPair(controllerInstance, currentMethod, argumentsMapping);

                            if (!this.Controllers.ContainsKey(requestMethod))
                            {
                                this.Controllers.Add(requestMethod, new Dictionary <string, ControllerActionPair>());
                            }

                            this.Controllers[requestMethod].Add(mapping, pair);
                        }
                    }
                }
                else if (type.GetCustomAttributes().Any(x => x.GetType() == typeof(ComponentAttribute)))
                {
                    foreach (Type parent in type.GetInterfaces())
                    {
                        this.Components.Add(parent, type);
                    }
                }
            }

            foreach (ControllerActionPair controllerActionPair in this.Controllers.Values.SelectMany(x => x.Values))
            {
                this.ResolveDependencies(controllerActionPair.Controller);
            }
        }