Manipulating RouteValueDictionary in RouteConstraint

115 Views Asked by At

Lets say I have a route; www.kunduz.com/stuff/something

where "something" is being checked against a route constraint;

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
    }
}

and lets say stuff has a structure like this

public class Stuff
{
     public int Id{get;set;}
     public string name {get;set;}
     //some other properties
}

Consider that object Stuff is not just an entry in DB, but more like a type. Like, if this was an e-commerce site, it could be "cars" or "furniture".

So what I'm doing is, I'm checking if "something" is really a valid "Stuff" on my route constraint. Then in my controller;

Public class SomeController
{
    public GetStuff(string stuffName)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }
}

Now, what I can also do is this

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
       values.Add("stuffId",stuff.Id");
    }
}

and get that Id I've added as a parameter on my controller action

    public GetStuff(int stuffId)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }

This would presumably increase performance and it makes more sense to me that I should avoid gettting stuff Id twice.

What my question is, is this a good practice? Since my URL doesnt contain sutffId, my controller action may be confusing for future developers looking at this code.

I would really appreciate some insight into this isssue.

Thank you.

0

There are 0 best solutions below