/// <summary> /// Initializes a new instance of the <see cref="T:Ceen.Mvc.ControllerRouter"/> class. /// </summary> /// <param name="config">The configuration to use</param> /// <param name="types">The types to use, must all derive from <see cref="T:Ceen.Mvc.Controller"/>.</param> public ControllerRouter(ControllerRouterConfig config, IEnumerable <Type> types) { if (types == null) { throw new ArgumentNullException($"{types}"); } if (config == null) { throw new ArgumentNullException($"{config}"); } // Make sure the caller cannot edit the config afterwards m_config = config.Clone(); var variables = new RouteParser(m_config.Template, !m_config.CaseSensitive, null).Variables; if (variables.Count(x => x.Key == m_config.ControllerGroupName) != 1) { throw new ArgumentException($"The template must contain exactly 1 named group called {m_config.ControllerGroupName}"); } if (variables.Count(x => x.Key == m_config.ActionGroupName) != 1) { throw new ArgumentException($"The template must contain exactly 1 named group called {m_config.ActionGroupName}"); } types = types.Where(x => x != null).Distinct().ToArray(); if (types.Count() == 0) { throw new ArgumentException($"No controller entries to load from \"{types}\""); } var wrong = types.FirstOrDefault(x => !typeof(Controller).IsAssignableFrom(x)); if (wrong != null) { throw new ArgumentException($"The type \"{wrong.FullName}\" does not derive from {typeof(Controller).FullName}"); } wrong = types.FirstOrDefault(x => x.IsAbstract || x.IsGenericTypeDefinition); if (wrong != null) { throw new ArgumentException($"The type \"{wrong.FullName}\" cannot be instantiated"); } m_routeparser = BuildParse(types.Select(x => (Controller)Activator.CreateInstance(x)).ToArray(), m_config); }
/// <summary> /// Initializes a new instance of the <see cref="T:Ceen.Mvc.ControllerRouter"/> class. /// </summary> /// <param name="config">The configuration to use</param> /// <param name="instances">The instances to use.</param> public ControllerRouter(ControllerRouterConfig config, IEnumerable <Ceen.Mvc.Controller> instances) { if (instances == null || instances.Any(x => x == null)) { throw new ArgumentNullException($"{instances}"); } if (config == null) { throw new ArgumentNullException($"{config}"); } // Make sure the caller cannot edit the config afterwards m_config = config.Clone(); var variables = new RouteParser(m_config.Template, !m_config.CaseSensitive, null).Variables; if (variables.Count(x => x.Key == m_config.ControllerGroupName) != 1) { throw new ArgumentException($"The template must contain exactly 1 named group called {m_config.ControllerGroupName}"); } if (variables.Count(x => x.Key == m_config.ActionGroupName) != 1) { throw new ArgumentException($"The template must contain exactly 1 named group called {m_config.ActionGroupName}"); } if (instances.Count() == 0) { throw new ArgumentException($"No instances were added as routes", nameof(instances)); } var duplicates = instances.GroupBy(x => x.GetType()).Where(x => x.Count() > 1); if (duplicates.Any()) { throw new ArgumentException($"The type \"{duplicates.First().Key}\" has {duplicates.First().Count()} instances"); } m_routeparser = BuildParse(instances, m_config); }