I'm currently developping an MVC framework on which I will be writing my websites. This is done through the usage of a starter kit.
In standard MVC, your routes are registered like the following:
/// <summary> /// Registers the routes. /// </summary> /// <param name="routes">The <see cref="RouteCollection" /> to which the routes will be added.</param> public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // All the required routes are placed here. routes.MapRoute("Website", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); }
But since it's a framework, there's a need to add routes to the collection, so I started with an interface:
public interface IMvcRouteExtender
{
#region Methods
/// <summary>
/// Register routes for the application.
/// </summary>
/// <param name="routes">The <see cref="RouteCollection"/> that contains all the required routes.</param>
void RegisterRoutes(RouteCollection routes);
#endregion
}
Now, in an implementation class, I can do the following:
public class CustomRouteConfiguration : IMvcRouteExtender
{
#region IMvcRouteExtender
/// <summary>
/// Registers the routes.
/// </summary>
/// <param name="routes">The <see cref="RouteCollection" /> to which the routes will be added.</param>
public void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Default", "pensions/save-and-pension", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
#endregion
}
In order to be sure there available, the original routeconfig is changed so that it searches for those interfaces and register them.
/// <summary>
/// Registers the routes.
/// </summary>
/// <param name="routes">The <see cref="RouteCollection" /> to which the routes will be added.</param>
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Get all the types in the references assembly that implement the 'IMvcRouteExtender' interface.
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(a => a.GetTypes())
.Where(t => typeof(IMvcRouteExtender).IsAssignableFrom(t) && !t.IsInterface);
// Register all the routes.
types.Select(type => Activator.CreateInstance(type) as IMvcRouteExtender).Each(x => x.RegisterRoutes(routes));
// All the required routes are placed here.
routes.MapRoute("Website", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
As you'll see above, I'm using reflection to load the types in which the routes are defined. This is working fine, but I don't like the overhead of using reflection.
Is there anyone who has an idea on how to make this more efficient? AKA, remove the dynamic type loading?
Since I'm writing a framework I cannot have a reference between routeConfig
and CustomRouteConfig
.
Also: Don't look at code semantics here, I'm just talking about a general idea to make the code more performant.