Esempio n. 1
0
    /// <summary>
    /// Initializes a new instance of <see cref="IChromelyRouteProvider"/>.
    /// </summary>
    /// <param name="routeParameterBinder">The <see cref="IChromelyModelBinder"/> instance.</param>
    /// <param name="dataTransferOptions">The <see cref="IChromelyDataTransferOptions"/> instance.</param>
    public DefaultRouteProvider(IChromelyModelBinder routeParameterBinder, IChromelyDataTransferOptions dataTransferOptions)
    {
        _routeParameterBinder = routeParameterBinder;
        _dataTransferOptions  = dataTransferOptions;

        RouteMap = new Dictionary <string, ControllerRoute>();
    }
Esempio n. 2
0
 public ControllerRoute(string name, dynamic del, IList <RouteArgument> argumentInfos, IChromelyModelBinder routeParameterBinder, IChromelyDataTransferOptions dataTransfers, bool isAsync, bool hasReturnValue)
 {
     Name                     = name;
     Delegate                 = del;
     RouteArguments           = argumentInfos;
     _routeParameterBinder    = routeParameterBinder;
     _dataTransfers           = dataTransfers;
     IsAsync                  = isAsync;
     HasReturnValue           = hasReturnValue;
     _routeArguments          = new object[argumentInfos.Count];
     _queryParameterArgs      = new Dictionary <string, object>();
     _propertyNameArgumentMap = new Dictionary <string, RouteArgument>();
 }
    public static void CreateAndRegisterRoutes(IChromelyRouteProvider routeProvider, ChromelyController controller, IChromelyModelBinder routeParameterBinder, IChromelyDataTransferOptions dataTransferOptions)
    {
        if (routeProvider is null || controller is null)
        {
            return;
        }

        var methodInfos = controller.GetType().GetMethods()
                          .Where(m => m.GetCustomAttributes(typeof(ChromelyRouteAttribute), false).Length > 0)
                          .ToArray();

        foreach (var methodInfo in methodInfos)
        {
            var attribute = methodInfo.GetCustomAttribute <ChromelyRouteAttribute>();
            var key       = RouteKeys.CreateActionKey(controller.RoutePath, attribute?.Path ?? string.Empty);
            if (!routeProvider.RouteExists(key))
            {
                routeProvider.RegisterRoute(key, ControllerRoutesFactory.CreateDelegate(controller, methodInfo, routeParameterBinder, dataTransferOptions));
            }
        }
    }
    private static ControllerRoute CreateDelegate(object instance, MethodInfo method, IChromelyModelBinder routeParameterBinder, IChromelyDataTransferOptions dataTransferOptions)
    {
        var args = method
                   .GetParameters();

        var arguments = new List <RouteArgument>();

        int lenght = args.Length;

        for (int i = 0; i < lenght; i++)
        {
            arguments.Add(new RouteArgument(args[i].Name, args[i].ParameterType, i));
        }

        var argTypes = args
                       .Select(p => p.ParameterType)
                       .Concat(new[] { method.ReturnType })
                       .ToArray();

        var newDelType = Expression.GetDelegateType(argTypes);
        var newDel     = Delegate.CreateDelegate(newDelType, instance, method);

        bool isAsync   = method.ReturnType.IsSubclassOf(typeof(Task));
        bool hasReturn = method.ReturnType != VoidType;

        // It is async method without return (void - System.Threading.Tasks.VoidTaskResult)
        if (method.ReturnType == typeof(Task))
        {
            isAsync   = true;
            hasReturn = false;
        }

        return(new ControllerRoute(method.Name, newDel, arguments, routeParameterBinder, dataTransferOptions, isAsync, hasReturn));
    }