5
\$\begingroup\$

I need a version of the mvc RouteValueDictionary that I can chain Add calls to, ie:

new RouteValueDictionaryExtended()
        .AddValue("controller", "Home")
        .AddValue("action", "Index")
        .AddValue("id", 3)

Is there a more description name I can use for this instead of just "Extended"?

   public class RouteValueDictionaryExtended : System.Web.Routing.RouteValueDictionary
    {
        public RouteValueDictionaryExtended() : base() { }
        public RouteValueDictionaryExtended(object values) : base(values) { }
        public RouteValueDictionaryExtended(System.Collections.Generic.IDictionary<string,object> dict) : base(dict) { }
        public RouteValueDictionaryExtended AddValue(string key, object obj)
        {
            var newDict = new RouteValueDictionaryExtended(this);
            newDict.Add(key, obj);
            return newDict;
        }
    }
\$\endgroup\$
  • \$\begingroup\$ Two questions: 1. Maybe I miss something, but how does 'new RouteValues().WithValue("controller", "Home").WithValue("action", "Index");' returns 'RouteValues'? The return type of 'WithValue' method is 'DictionaryBuilder<RouteValueDictionary>', not 'RouteValues'. 2. What stops you from using extension methods? \$\endgroup\$ – Ivan Aug 18 '11 at 14:12
7
\$\begingroup\$

I would actually recommend you to use the (Constructor) Builder pattern if you are only doing this for easier object initialization:

public class RouteValueDictionaryBuilder
{
    private List<Tuple<string, object>> values_ = new List<Tuple<string, object>>();

    public RouteValueDictionaryBuilder WithValue(string key, object obj)
    {
        values_.Add(Tuple.Create(key, obj));
        return this;
    }

    public RouteValueDictionary Build()
    {
        var dict = new RouteValueDictionary();
        foreach (var value in values_)
        {
            dict.Add(value.Item1, value.Item2);
        }
        return dict;
    }
}

To use:

var dict = new RouteValueDictionaryBuilder()
    .WithValue("controller", "Home")
    .WithValue("action", "Index")
    .WithValue("id", 3)
    .Build();

This avoids the higher coupling (inheritance) between the dictionary and its subclass, which is there only to help with initializing the dictionary.

\$\endgroup\$
5
\$\begingroup\$

If you need this class only to be able to chain 'Add' calls, maybe it would be better to just create an extension method:

public static class RouteValueDictionaryExtensions
{
    public static RouteValueDictionary AddValue(this RouteValueDictionary routeValueDictionary, string key, object obj)
    {
        routeValueDictionary.Add(key, obj);
        return routeValueDictionary;
    }
}

And use it like:

 new RouteValueDictionary()
     .AddValue("controller", "Home")
     .AddValue("action", "Index")
     .AddValue("id", 3);

If you still need a separate class and its only purpose is to add this chaining ability, then express it in the name like 'ChainableRouteValueDictionary' or 'FluentRouteValueDictionary'.

\$\endgroup\$
  • \$\begingroup\$ I don't think an extension method would work in my context, but I Fluent was exactly what I was looking for thank you! \$\endgroup\$ – asawyer Aug 17 '11 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.