views:

111

answers:

1

Imagine this case:

A color has an id. Easy enough.
The same color can, depending on the user's language pref, search query and personal preference have a different display name.
This display name is not defining, but it needs to be passed on to each page so the user is confronted with the same label every time the color is mentioned.
Furthermore, (and just for keeping this explanation brief, go with me on this one) the name can also be blank (so the parameter is optional).

Since there are many of such parameters and the parameters are optional and I don't want to push all these optional parameters to the ugly query string, I have made a single URL parameter to identify the color that basically puts the color id and the optional parameters in one parameter, separated with a custom delimiter.

Examples:

  • Color.mvc/display/123456--BorisRed--AnotherParameter
  • Formula.mvc/display/123456--BorisRed--AnotherParameter/987654

I have a helper method that generates this string and one that parses this string.

It's working out fine so far, but I feel there is probably a better way to do this. Any suggestions?

A: 

Looks OK to me; you might want to see whether a "model binder" could be used in the parsing layer, so that you use just {id} in the route, and have SomeAction(MyColor id), and have the model-binder automatically parse the id. Alternatively (and perhaps more simply), I seem to recal that it will also use TypeConverter here - so perhaps write a TypeConverter for your color class/struct (MyColor above).

Example converter:

[TypeConverter(typeof(MyColorConverter))]
public class MyColor
{
    public int Id { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

class MyColorConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context,
         Type sourceType)
    {
        return sourceType == typeof(string)
            || base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, 
        System.Globalization.CultureInfo culture, object value)
    {
        if (value != null && value is string)
        {
            string[] parts = ((string)value).Split(new string[] {"--"},
               StringSplitOptions.None);
            MyColor color = new MyColor();
            if(parts.Length > 0) color.Id = int.Parse(parts[0]);
            if(parts.Length > 1) color.Name = parts[1];
            return color;
        }
        return base.ConvertFrom(context, culture, value);
    }
    public override bool CanConvertTo(ITypeDescriptorContext context,
         Type destinationType)
    {
        return destinationType == typeof(string)
            || base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture, object value,
        Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            MyColor color = (MyColor)value;
            return color.Id + "--" + color.Name;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}
Marc Gravell
Hmm, so the paste-multiple-things-in-one-string doesn't make you shudder? I could indeed put the whole thing in a typeconverter. Currently I have the logic in a helper method I call manually. So no sweat there.
borisCallens
Marc Gravell