15.11.08

In search of dynamic cohesion via extension methods

Extension methods are a new language construct that allow the addition of new method definitions to an existing type.
One particular scenario where they have proved useful is in extending the HtmlHelper type that allows rendering of controls in ASP.NET MVC. This encourages a belated type of cohesion, i.e., all concerns to do with control rendering in the view must reside in the HtmlHelper.


public static string CommandBuilder(this HtmlHelper helper, string controller, string action, string commandName, int id)
{
return string.Format("<a href='/{0}/{1}/{2}'>{3}</a>", controller, action, id, commandName);
}


However one wonders whether the same could not be achieved through normal type inheritance. If there are breaking changes in the implementation from Microsoft, then be prepared to break.

No comments: