horsdal / Nancy.Linker

Simple URI builder for named Nancy routes
MIT License
13 stars 2 forks source link

Allow to retrieve a RouteDescription by the route name #9

Open a-bronx opened 9 years ago

a-bronx commented 9 years ago

I create hypermedia links in models returned by a controller, and these links contain not only rel= and href= attributes, but also a method= one, providing an HTTP methods for the given route. To keep DRY, I'd like to avoid passing method names manually as strings in every link constructor. The RouteDescription already contains all required information, and I'd like to use it.

I can collect all named routes in a separate component (what I do right now), but the ResourceLinker already keeps them in AllRoutes member, so it would be nice to reuse it somehow. What about making the AllRoutes member public, or provide a method to retrieve a RouteDescription by a route name?

P.S. Note that the current implementation of AllRoutes stores all routes, while using only named ones. It may be also good to change the AllRoutes to a dictionary, for more effective lookups.

private Dictionary<string, RouteDescription> namedRoutes = null;
this.namedRoutes = this.routesProvider.GetCache()
                        .SelectMany(pair => pair.Value.Select(tuple => tuple.Item2))
                        .Where(route => !string.IsNullOrWhiteSpace(route.Name))
                        .ToDictionary(route => route.Name);
horsdal commented 9 years ago

Adding a method for getting a RouteDescriptor by name sound like a good idea. So does the optimization in the PS :smile:

Are you up for creating two PRs - one for each change?