从打开的DataReader中返回新对象-可以吗?

问题描述

我看到了以下代码

public static ConsTypeCategory GetConsCategory(int providerId,int categoryId) {
    const string select =
        @"SELECT CategoryId,CategoryName,ExpectedDurationMins 
        FROM constypecategories
        WHERE ProviderId = @ProviderId
            AND CategoryId = @CategoryId
        LIMIT 1";
    MysqLParameter[] MysqLParameters = {
        new MysqLParameter("ProviderId",providerId),new MysqLParameter("CategoryId",categoryId)
    };
    using (MysqLDataReader dr = MysqLHelper.ExecuteReader(DB.ConnectionString,select,MysqLParameters)) {
        while (dr.Read()) {
            return new ConsTypeCategory(
                dr.GetInt32("CategoryId"),dr.GetString("CategoryName"),dr.GetInt32("ExpectedDurationMins")
            );
        }
    }
    return null;
}

...并立即认为从datareader循环中返回“新”对象是错误的。但是,为了找到该观点的理由,我变得空虚。

是否有充分的理由不从“一会儿”之内返回?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)