RouteContext as constructor parameter or method parameter in ASP.NET vNext I have this simple class where I load an object from the database and store it into the RouteContext.DataTokens because I need to access it through out the request.

In this example the RouteContext is injected via the constructor but I'm not sure that's the right thing to do here, maybe the RouteContextshould be a method parameter instead?

public class RouteResolverTrie : IRouteResolverTrie
{
    private readonly IDocumentStore documentStore;
    private RouteContext context;

    public RouteResolverTrie(RouteContext context, IDocumentStore documentStore)
    {
        this.documentStore = documentStore;
        this.context = context;
    }

    public Trie LoadTrie()
    {
        if(context.DataTokens.ContainsKey("some key")) {
            return context.DataTokens["trie"] as Trie;
        }
        else {
            using(var session = documentStore.OpenSession()) {
                var trie = session.Load&lt;Trie&gt;("trie/1");
                context.DataTokens["trie"] = trie;
                return trie;
            }
        }
    }
}


This is how the routing works in vNext and this is my first entry point. As you can see I use LoadTrie() in the method RouteAsync which in turn uses the RouteContext to store temporary data through out the request. So, the question is, should I instantiate the DefaultRouter with a new RouteResolverTrie that takes the current RouteContext as a constructor parameter or should it be a method parameter?

public class DefaultRoute : IRouter
{
    private readonly IRouter _target;
    private IRouteResolverTrie _routeResolverTrie;

    public DefaultRoute(IRouter target, IRouteResolverTrie routeResolverTrie) {
        _target = target;
        _routeResolverTrie = routeResolverTrie;
    }

    public string GetVirtualPath(VirtualPathContext context)
    {
        return null;
    }

    public async Task RouteAsync(RouteContext context)
    {
        var trie = _routeResolverTrie.LoadTrie();
    }
}
Since you need something that persists for the duration of request, a better place would be to add to RequestContext. So there are couple of ways to handle this more cleanly:

Add a base controller and add additional data to RequestContext:

protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
    // get additional data
    ...

    // add them to routeDate
    requestContext.RouteData.Values.Add(key, value);
    ...

    base.Execute(requestContext);
}


Add your own custom RouteHandler and add additional data to RequestContext:

public class CustomRouteHandler : MvcRouteHandler 
{ 

    protected override IHttpHandler GetHttpHandler(RequestContext requestContext){
           // add to HttpContext
           requestContext.HttpContext.Items["Custom"] = data;

           // or RouteData
           requestContext.RouteData.Values.Add(key, value);
        }
        return base.GetHttpHandler(requestContext);
    }

}

// define route with custom handler 
Route customRoute = new Route (
    url: ...,
    defaults: null,
    constraints: ...,
    routeHandler: new CustomRouteHandler()
});

routes.Add(customRoute);