partial modifier?

Dec 14, 2011 at 8:32 AM

Is it possible to add 'partial' keyword to generated classes? This can be useful in some scenarios, especially in helpers. For example, partial definition allows to add some C# methods, that can be used inside razor code.

Coordinator
Dec 14, 2011 at 5:13 PM
Edited Dec 14, 2011 at 5:13 PM

We generate Mvc helpers as static types, so that you could write your helpers as extension methods to HtmlHelper. Partials wouldn't work here. For views, I guess it would make more sense to simply call a helper rather than have code across pages.

Are there specific scenarios you are trying to address here?

Dec 14, 2011 at 11:04 PM

1) I don't need to extend HtmlHelper, because there is no semantic links to it.

2) Of course, I can create separate static class, but it increases number of classes without any needs and adds explicit class names to razor code.

IMHO, it isn't require too much work to add partial to classes, and I don't see any side effects of this.

For example -

Now:

@helper Helper()
{
  var data = Helper2.LoadData();
  ...
}

With partial

@helper Helper()
{
  var data = LoadData();
  ...
}
Second sample is cleaner, isn't it?

Coordinator
Dec 15, 2011 at 4:26 PM

Sure. Making sure you are aware of @function blocks. You can write regular C# code inside one of those.

Additionally, assuming you are working with an Mvc app, for partials to work with your helper once we add a directive for partials, you would need to set the generator for the file as  WebPagesHelper (@* Generator : WebPagesHelper). As I said earlier, we modify helpers generated in Mvc applications to be static types.