我应该使用扩展方法来帮助从 DataRow/Table 创建模型对象吗?
我目前使用一些重复的代码 - 更具体地说,处理从 DataRow 和 DataTable 创建模型对象的代码。我认为创建扩展方法来减少重复代码并添加一些语法糖是安全的。
我之前有什么(简化):
public List<MyObject> GetThings(){
DataSet dataSet = SomeDatabaseCall();
var objects = new List<MyObject>();
foreach(DataRow row in dataSet.Tables[0].Rows){
//Process Row, create object, add to objects
}
return objects;
}
public MyObject GetThing(int id){
DataSet dataSet = SomeDatabaseCall(id);
DataRow row = dataSet.Tables[0].Rows[0];
//Process Row, create object, return it
}
之后我想要什么:
public List<MyObject> GetThings(){
DataSet dataSet = SomeDatabaseCall();
return dataSet.ToMyObjects(); //Internally calls the ToMyObject for each row
}
public MyObject GetThing(int id){
DataSet dataSet = SomeDatabaseCall(id);
DataRow row = dataSet.Tables[0].Rows[0];
return row.ToMyObject();
}
问题:
有人向我指出,以这种方式使用扩展方法很危险,我应该使用采用 DataRow
的简单静态函数来处理数据(本质上是不带 this
参数的扩展方法)。
问题:
扩展方法在这种情况下有意义吗?为什么这种做事方式会被认为是危险的?
I currently work with some code that is repeated - more specifically, code that deals with the creation of model objects from DataRow
s and DataTable
s. I thought it would be safe to create Extension Methods to cut down on this repeated code, and add some syntactic sugar.
What I had Before (simplified):
public List<MyObject> GetThings(){
DataSet dataSet = SomeDatabaseCall();
var objects = new List<MyObject>();
foreach(DataRow row in dataSet.Tables[0].Rows){
//Process Row, create object, add to objects
}
return objects;
}
public MyObject GetThing(int id){
DataSet dataSet = SomeDatabaseCall(id);
DataRow row = dataSet.Tables[0].Rows[0];
//Process Row, create object, return it
}
What I want After:
public List<MyObject> GetThings(){
DataSet dataSet = SomeDatabaseCall();
return dataSet.ToMyObjects(); //Internally calls the ToMyObject for each row
}
public MyObject GetThing(int id){
DataSet dataSet = SomeDatabaseCall(id);
DataRow row = dataSet.Tables[0].Rows[0];
return row.ToMyObject();
}
The problem:
It was pointed out to me that using Extension Methods in this way is dangerous, and that I should use simple static functions that take a DataRow
instead to process the data (essentially an Extension Method without the this
parameter).
The question:
Would Extension Methods make sense in this scenario? And why would this way of doing things be considered dangerous?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展方法
并不比创建静态
方法更危险,因为扩展方法
编译为静态方法。它们只是为程序员带来好处的语法糖。您可以自己比较IL
,但这里有一篇文章和一个示例,可以帮助您说服您的同事扩展方法
并不危险:扩展方法如何工作?Extension methods
are no more or less dangerous than making astatic
method becauseextension methods
compile to static methods. They are just syntactic sugar for the programmers benefit. You could compare theIL
on your own but here is an article with an example that might help you convince your coworkerextension methods
aren't dangerous: How do Extension Methods work?我不认为这样做有什么危险;只要您能够控制所有代码,并且数据集的布局就不会在您的控制下发生改变。这似乎正是扩展方法的设计目的。
我很想知道这是“危险”用途这一说法背后的原因。
I don't see anything dangerous about doing this; as long as you have control over the all of the code and the layout of your DataSet isn't going to be changed out from underneath you. This seems exactly like the kind of thing that Extension Methods were designed for.
I'd be curious to hear the reasoning behind the statement that this was a "dangerous" use.
如果您将扩展方法放入特定于您的项目的库中,那么您就不会冒用您的特定扩展玷污其他项目的风险。我也不明白这有什么危险。
更新:好的,我明白你的观点了,@Pwninstein:你调用扩展的
DataRow
很可能是任何东西。 但是,拥有一个采用DataRow
作为参数来创建对象的方法不再危险。If you put the extension methods in a library that is specific to your project then you won't risk sullying other projects with your very specific extensions. I don't see how it's dangerous either.
UPDATE: Okay, I see your point, @Pwninstein: the
DataRow
that you call the extension on could very well be ANYTHING. However, having a method that takes aDataRow
as an argument to make your object is no more dangerous.