我应该使用扩展方法来帮助从 DataRow/Table 创建模型对象吗?

发布于 2024-10-03 13:23:37 字数 1177 浏览 4 评论 0原文

我目前使用一些重复的代码 - 更具体地说,处理从 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 DataRows and DataTables. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任性一次 2024-10-10 13:23:37

扩展方法并不比创建静态方法更危险,因为扩展方法编译为静态方法。它们只是为程序员带来好处的语法糖。您可以自己比较IL,但这里有一篇文章和一个示例,可以帮助您说服您的同事扩展方法并不危险:扩展方法如何工作?

Extension methods are no more or less dangerous than making a static method because extension methods compile to static methods. They are just syntactic sugar for the programmers benefit. You could compare the IL on your own but here is an article with an example that might help you convince your coworker extension methods aren't dangerous: How do Extension Methods work?

浅笑依然 2024-10-10 13:23:37

我不认为这样做有什么危险;只要您能够控制所有代码,并且数据集的布局就不会在您的控制下发生改变。这似乎正是扩展方法的设计目的。

我很想知道这是“危险”用途这一说法背后的原因。

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.

栀子花开つ 2024-10-10 13:23:37

如果您将扩展方法放入特定于您的项目的库中,那么您就不会冒用您的特定扩展玷污其他项目的风险。我也不明白这有什么危险。

更新:好的,我明白你的观点了,@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 a DataRow as an argument to make your object is no more dangerous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文