Consider the following (Microsoft-sanctioned) code:
<% using (Html.Form<HomeController>(action=>action.Index())) { %>
<input type="text" id="search" />
<input type="button" value="Submit" />
<% } %>
The using
statement has but one purpose here: to add a </form>
tag to the end of the generated HTML.
This feels dirty to me, for what I hope are obvious reasons:
- It's not
using
's raison d'etre. - It deliberately causes side-effects.
- It's not intuitive that
Html.Form
would implementIDisposable
. - The
using
statement is not actually required, butHtml.Form
will break without it (unless you emit</form>
yourself, which kinda misses the whole point).
This is not the only abuse that people have heaped onto the using
pattern.
The question is, is there a viable alternative that provides the same stylistic/readability benefits as using
, without making you feel like taking a shower afterwards?