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;
 }
 }
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 17, 2011 at 14:58
\$\endgroup\$
1
  • \$\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\$ Commented Aug 18, 2011 at 14:12

2 Answers 2

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.

answered Aug 18, 2011 at 7:31
\$\endgroup\$
0
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'.

answered Aug 17, 2011 at 15:15
\$\endgroup\$
1
  • \$\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\$ Commented Aug 17, 2011 at 15:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.