2

I'm creating a set of helpers for rendering compatible Twitter Bootstrap html. As I see it, I have two options when it comes to how to group these methods together:

  • Extend the HtmlHelper prefixing the methods with TB
  • Create a new class TBootHelper which contains the methods

In the second case, to get the TBoot helper available, the developer would add

<pages pageBaseType="Twitter.Bootstrap.Mvc.TBootViewPage">

To it's ~/Views/web.config (as pointed by @darin)

Or instantiate the helper when it's needed

@using Twitter.Bootstrap.Mvc
var TBoot = new TBootHelper<TModel>(Html);

My question is, Should I create a TBootHelper class or just add methods to the HtmlHelper?

Saulo Vallory
  • 959
  • 9
  • 32

1 Answers1

3

I'd go with creating a custom TBootHelper and a custom base view that all views will inherit from and which will have a property of type TBootHelper.

And instead of forcing the developer to add @inherits Twitter.Bootstrap.Mvc.TBootViewPage<TModel> to every single Razor template in which he wants to use this custom helper, I would add it to the ~/Views/web.config file, once and for all:

<pages pageBaseType="Twitter.Bootstrap.Mvc.TBootViewPage">

and then in the views:

@model MyViewModel
@TBoot.Foobar()
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I think that part of the text made my question confusing. Make the View Inherit from `TBootViewPage` **WILL** be optional. So the developer can add it to web.config **IF** he wants to. This way he doesn't have to instantiate a `TBootHelper` on every view. The question is: Should I create a `TBootHelper` class or just add methods to the `HtmlHelper`? I'll clarify the question – Saulo Vallory Feb 26 '12 at 20:54
  • @svallory, that's a subjective question. Don't expect answers to it. Do whatever you feel comfortable with. Add a TBootHelper if you want to group your methods, add extensions to the HtmlHelper for easier discovery. Up to you and your scenario really. – Darin Dimitrov Feb 26 '12 at 20:57
  • everybody seems to use extension methods. Do you see any traps or issues I could run into by creating another class? – Saulo Vallory Feb 26 '12 at 21:00
  • @svallory, that will depend on what you are trying to design and achieve. You will have to provide far greater details about your system, goals, etc... and even after that it might be difficult to answer this question. That's almost like asking should I use .NET or Java to design my application. All I can tell you is: choose the method that you feel fits best for your scenario. – Darin Dimitrov Feb 26 '12 at 21:01
  • I don't see it that way. If I knew the dependencies of `HtmlHelper` I would evaluate it. e.g. Does `HtmlHelper` use any internal resource which I won't have access if I create another class? – Saulo Vallory Feb 26 '12 at 21:05
  • @svallory, no internal resource that you can't do without. Also don't forget that in an extension method you cannot access the internal resources of HtmlHelper so I don't see why are you even asking this question. You can access for example protected members only if you are inheriting from HtmlHelper. – Darin Dimitrov Feb 26 '12 at 21:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8243/discussion-between-svallory-and-darin-dimitrov) – Saulo Vallory Feb 26 '12 at 21:12