Applying the builder pattern to the .Net MVC TagBuilder
Sometimes I’m a nitpicker. Actually I’m a nitpicker 24/7, but I can’t really say that on a blog, can I? So today’s nitpicking is about C#’s TagBuilder. This class allows you to build html without writing a single ‘<’ or ‘>’. Using this class usually boils down to something like this: // this will create <button> var input = new TagBuilder("button"); // Set some attributes input.Attributes.Add("name", "property.name"); input.Attributes.Add("id", "property_id"); // some classes for markup input.AddCssClass("btn btn-primary"); // equivalent to jQuery's $("property_id").text("Click me!"); input.InnerHtml = "Click me!"; However, wouldn’t it be nicer if we could say: ...