views:

42

answers:

4

I recently found out about C# extension methods and wrote this one:

/// <summary>
/// Short-hand for setting the data source and binding it.
/// </summary>
/// <param name="me">The control to set and bind the data source of.</param>
/// <param name="dataSource">The data source.</param>
public static void BindTo(this DataBoundControl me, IEnumerable dataSource)
{
    me.DataSource = dataSource;
    me.DataBind();
}

What do you guys think? Is this a reasonable extension method to use in a professional ASP.NET project?

+2  A: 

Not really. It saves you 1 method call, at the result of introducing an unknown approach. And you really shouldn't need to be setting the binding all the time anyway (depending on how you do it, I suppose, I prefer to do it in the markup).

So no, I wouldn't consider this appropriate. But then again, I'm quite against extension methods in general, so I'm clearly biased, FWIW.

Noon Silk
A: 

Not worth the effort, imho.

TomTom
A: 

simplicity makes the code clean =)

hallie
A: 

I think there is value to this sort of extension method, but only as part of a larger helper class. I've been in situations where I've done exactly the same thing - normally for editable data grids where I want to bind drop down lists in each cell to a particular lookup list. It makes the code much more readable, specially if you are programmatically generating the databoundcontrol and the datasource instead of having everyting at design time.

one thing though, I would change the databoundcontrol parameter name from me to something else, maybe boundControl ?

Mauro