开发者

C#: Is this a reasonable extension method for an ASP.NET project?

开发者 https://www.devze.com 2022-12-22 13:47 出处:网络
I recently found out about C# extension methods and wrote this one: /// <summary> /// Short-hand for setting the data source and binding it.

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=开发者_StackOverflow"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?


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.


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 ?

0

精彩评论

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