Extending MVC Contrib MenuBuilder

By Deathraven on Tuesday 28 September 2010 09:37 - Comments (4)
Category: MVC, Views: 5.551

Today i am going to talk about MVCContrib's MenuBuilder. I started using it in a project of mine because it did exactly what i wanted it to and it saved me the effort of writing something like that myself.



However, like always in Software Development, requirements change and i suddenly had the need to be able to supply attributes when creating a MenuItem.
I already knew that the MenuBuilder was unable to do that so nothing new there. A couple of WTF's and moments of frustrations later and i had something, its not pretty, but it works.

Lets start by taking a look at how the MenuBuilder works. The entry point of this thing is a static class called Menu. It contains the helper methods to create a MenuItem and is the preferred place where we want to add our new methods.
The fact that its a big static monster is a royal PITA. There is no subclassing a static class, or adding extension methods for that matter.

I could have created my own non-static helper class for creating MenuItems and building the tree. However my employer doesn't pay me to do overkill. So i created my own static monster :P


C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public static class MenuBuilder 
    {
        public static MenuItem Link(string url, object attributes)
        {
            return Link(url, null, null, attributes);
        }

        public static MenuItem Link(string url, string title, object attributes)
        {
            return Link(url, title, null, attributes);
        }

        public static MenuItem Link(string url, string title, string icon, object attributes)
        {
            var item = new MenuItemWithAttributes();
            if (attributes != null)
            {
                item.HtmlAttributes = new RouteValueDictionary(attributes);
            }
            item.Title = title;
            item.ActionUrl = url;
            item.Icon = icon;
            return AddDefaults(item);
        }

        public static MenuItem AddDefaults(MenuItem item)
        {
            item.AnchorClass = Menu.DefaultAnchorClass;
            item.IconClass = Menu.DefaultIconClass;
            item.ItemClass = Menu.DefaultItemClass;
            item.IconDirectory = Menu.DefaultIconDirectory;
            item.DisabledClass = Menu.DefaultDisabledClass;
            item.ShowWhenDisabled = Menu.DefaultShowWhenDisabled;
            item.SelectedClass = Menu.DefaultSelectedClass;
            if (item is MenuList)
            {
                ((MenuList)item).ListClass = Menu.DefaultListClass;
            }
            return item;
        }
    }



MenuItemsWithAttributes isn't that exiting.


C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MenuItemWithAttributes : MenuItem
    {
        public IDictionary<string, object> HtmlAttributes = new Dictionary<string, object>();

        protected override string RenderLink()
        {
            CleanTagBuilder builder = new CleanTagBuilder("a");
            builder.MergeAttributes(HtmlAttributes);
            if (this.ItemDisabled)
            {
                builder.AddCssClass(this.DisabledClass);
            }
            else
            {
                 builder.MergeAttribute("href", this.ActionUrl);
            }
            if (this.IsItemSelected())
            {
                builder.AddCssClass(this.SelectedClass);
            }
            builder.MergeAttribute("title", this.HelpText);
            builder.InnerHtml = builder.InnerHtml + this.RenderIcon() + this.RenderTitle();
            return builder.ToString(TagRenderMode.Normal);
        }
    }



Why the static helper class was used in the first place? I don't know, maybe to create a more fluent API. However that can be achieved by applying normal OO design as well.
For the time being this solution suits my needs. I still might take the effort of building a non-static helper class in the future. If i do, ill be sure to post it here so it might safe someone else the trouble of doing it himself.

Volgende: The age of NuGet 01-'11 The age of NuGet
Volgende: Spark and MvcContrib and the 'The type or namespace name 'FluentHtml' does not exist' error 08-'10 Spark and MvcContrib and the 'The type or namespace name 'FluentHtml' does not exist' error

Comments


By Tweakers user -RetroX-, Tuesday 28 September 2010 13:54

Coding -> “10 percent inspiration, 90 percent transpiration” }:O

By Orunpegan, Sunday 21 July 2013 01:43

Super excietd to see more of this kind of stuff online.

By Echan, Tuesday 23 July 2013 23:10

Bong gm . Bong uaptde tov jeng ey . Me kom som dom nam jg ngob . Mean egg grape ng milk . Bong kom pleg uaptde lern pg na .Bye bong . Me nerg bong srey me 1 name arith 1 tet eri . Pye gaga 0

By car insurance in florida, Sunday 8 September 2013 03:46

Way to go on this essay, helped a ton.

Comments are closed