开发者

ASP.NET MVC helper extensions & separation of concerns

When implementing a helper extension for a similar navigation widget to the one described in this article I notice that my HTML is now tied up in C# code, so if I want to re-skin the site with a new menu control that needs slightly different markup (e.g <ul class="foo">...</ul> or distinct class names for nested <li> tags) I now have to edit both the helper extension and the view.

Using a helper extension is quicker and easier, but does this not violate separation of con开发者_JS百科cerns? What are your experiences of maintaining such code? I'm fairly new to Microsoft MVC so forgive me if I'm missing some point here.


A well-designed HTML Helper should be versatile and have very loose opinions about how it builds its HTML. You can add overridable options to the extension method so that the rendering is as configurable as necessary. If you have new requirements and your existing helper isn't flexible enough, extend it to accommodate the new requirements.


I think you are right and have found similar thing. It's the quicker / easier now vs. potential future pain "if" you need to change things in the future. You could possibly make your "helper" read in a template that it uses, so you could change that, restart the web app and see your changes appear. However, that increases complexity too. I would say a lot of agile says do the easier / quicker stuff now and understand that in the future if your requirements change then revisit it then. Who knows, it may never happen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜