简体   繁体   English

跨N层使用“使用语句”

[英]“Using Statement” across N-Layer

I've recently inherited a .Net Web App with a 4 layer setup. 我最近继承了一个带有4层设置的.Net Web App。 As per normal with these situations, I have no documentation and no contact with the prior devs. 按照这些情况的正常情况,我没有文档,也没有与之前的开发人员联系。 The members of previous team for the app had vastly different habits, and I'm trying to sort through these as I refactor/standardize the code. 该应用程序的前一个团队的成员有着截然不同的习惯,我正在尝试对这些代码进行重构/标准化。 I ran into something I haven't seen before (and I'm not able to find anywhere or anyone implementing this as a practice ) in which one/many of the developers wrap all DataSets in a using statement as it's returned through the layers. 我遇到了一些我以前从未见过的东西(我无法找到任何地方或任何人实现这个作为练习),其中一个/多个开发人员将所有DataSet包装在using语句中,因为它通过层返回。 As soon as I saw it I wanted to junk it. 我一见到它就想把它扔掉。 In many of these implementations the readability is shot. 在许多这些实现中,可读性被拍摄。 Then I started questioning myself because I can't imagine someone doing this just because. 然后我开始质疑自己,因为我无法想象有人这样做是因为。 I understand the desire to dispose of objects as they're used, but this just seems like someone went overboard. 我理解在使用对象时处理对象的愿望,但这似乎有人过火了。 Does anyone know of a solid reason to keep this implemented? 有没有人知道这个实施的坚实理由?

Here's a little TLDR example of what's being done. 这是一个关于正在做什么的TLDR示例。

Presentation: 介绍:

using (DataSet ds = objCustomerFBO.FetchCustomerInfo(base.KeyID))
{
    grdCustomerInfo.DataSource = ds;
}
grdCustomerInfo.DataBind();

Object: 宾语:

public DataSet FetchCustomerInfo(int keyID)
{
    using (DataSet ds = objCustomerEBO.FetchCustomerInfo(keyID);
    {
         return ds;
    }
}

Business: 商业:

public DataSet FetchCustomerInfo(int keyID)
{
    SqlParameter arrSqlParam = new SqlParameter[1];
    arrSqlParam[0] = new SqlParameter("@CustomerID", SqlDbType.Int);
    arrSqlParam[0].Value = keyID;

    using (DataSet ds = CommonEBO.ExecuteDataSet("FetchCustomerInfo", arrSqlParam);
    {
         return ds;
    }
}

Data (after filtering through overloads): 数据(通过重载过滤后):

public static DataSet ExecuteDataSet(string strCommandText, SqlParameter[] SqlParams, CommandType commandtype, string strConnectionString)
    {
        DataSet ds = new DataSet();
        try
        {
            using (SqlDataAdapter oAdpt = new SqlDataAdapter(CMSOnlineDH.CreateCommand(strCommandText, SqlParams, commandtype, strConnectionString)))
            {
                oAdpt.SelectCommand.Connection.Open();
                oAdpt.Fill(ds);
            }
        }
        catch { throw; }
        return ds;
    }

I don't see the point in the using statements that wrap around a return of the instantiated object. 我没有看到包含实例化对象returnusing语句中的要点。 These I would remove as it just hinders readability. 这些我会删除因为它只会妨碍可读性。

The first example doesn't make much sense either - since calling DataBind will need to operate over the bound DataSet , doing the binding outside of the using statement makes very little sense. 第一个例子也没有多大意义 - 因为调用DataBind需要在绑定的DataSet上操作,所以在using语句之外进行绑定没有多大意义。 I would have put that within the using statement. 我会把它放在using语句中。

The only usage that appears correct to me is with the DataAdapter (last example). 对我来说唯一正确的用法是DataAdapter (最后一个例子)。

In all it looks like someone took the advice to always use using statements for objects implementing IDisposable a bit overboard. 总之,看起来有人接受了建议,总是使用using语句实现IDisposable有点过分。

It isn't wrong as you've inherited it and its working. 这是没有错的,因为你继承了它并且它的工作。 I believe ExecuteDataSet could of been the only place needed for that using statement. 我相信ExecuteDataSet可能是该using语句所需的唯一地方。 Based on the information given of course. 根据当然提供的信息。 If however, you've stripped out snippets of connection information and other resources that might be a different story. 但是,如果您已经删除了可能是另一个故事的连接信息和其他资源的摘要。

For instance, what was the purpose of the presentation layer being wrapped around a using statement that gets its value from a returned dataset??? 例如,表示层的目的是围绕一个using语句,该语句从返回的数据集中获取其值?

What exactly does this impact? 这究竟是什么影响? I think ultimately it affects other programmers (As you have posted here) in regards to readability and understanding what exactly is going in. It's additional noise as your eyes sift through all that code. 我认为最终它会影响其他程序员(正如你在这里发布的)关于可读性和理解究竟是什么。当你的眼睛筛选所有代码时,它会产生额外的噪音。

看起来像处理数据集没有任何好处,因为IDisposable它只是没有实现( 看看这里 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM