感觉 Data Access Application Block(DAAB) 里也有可能写得不太好的地方
昨天下载了博客园的代码,里面有一个
Data\SqlServer.cs
我不清楚是不是 MS DAAB 里的原样文件。不过前面有声明如下:
我阅读到下面一段代码:
看上去写得比较重复。我觉得完全可以重构为:
而且我向后看了,这个 cmdText 对象在这个大的方法里再也没有使用过,所以对后面不会有影响。
另外大致看了一下,感觉 GetProcedureTokens 这个方法也可以写得更好一些。
从这个看我觉得里面有一些代码也不像想象的那么完美。说的不对的地方请各位指正。
Data\SqlServer.cs
我不清楚是不是 MS DAAB 里的原样文件。不过前面有声明如下:
// ===============================================================================
// Microsoft Data Access Application Block for .NET 3.0
//
// SqlServer.cs
//
// This file contains the implementations of the AdoHelper supporting SqlServer.
// Microsoft Data Access Application Block for .NET 3.0
//
// SqlServer.cs
//
// This file contains the implementations of the AdoHelper supporting SqlServer.
我阅读到下面一段代码:
// set up the command string. We use sp_procuedure_params_rowset to get the parameters
if (database != null)
{
cmdText = string.Concat("[", database, "]..sp_procedure_params_rowset");
if (server != null )
{
cmdText = string.Concat(server, ".", cmdText);
}
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand(cmdText, cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand(cmdText, cmd.Connection);
}
}
else
{
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection);
}
}
if (database != null)
{
cmdText = string.Concat("[", database, "]..sp_procedure_params_rowset");
if (server != null )
{
cmdText = string.Concat(server, ".", cmdText);
}
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand(cmdText, cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand(cmdText, cmd.Connection);
}
}
else
{
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand("sp_procedure_params_rowset", cmd.Connection);
}
}
看上去写得比较重复。我觉得完全可以重构为:
// set up the command string. We use sp_procuedure_params_rowset to get the parameters
if (database != null)
{
cmdText = string.Concat("[", database, "]..sp_procedure_params_rowset");
if (server != null )
{
cmdText = string.Concat(server, ".", cmdText);
}
}
else
{
cmdText = "sp_procedure_params_rowset";
}
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand(cmdText, cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand(cmdText, cmd.Connection);
}
if (database != null)
{
cmdText = string.Concat("[", database, "]..sp_procedure_params_rowset");
if (server != null )
{
cmdText = string.Concat(server, ".", cmdText);
}
}
else
{
cmdText = "sp_procedure_params_rowset";
}
// be careful of transactions
if (trnSql != null )
{
newCommand = new SqlCommand(cmdText, cmd.Connection, trnSql);
}
else
{
newCommand = new SqlCommand(cmdText, cmd.Connection);
}
而且我向后看了,这个 cmdText 对象在这个大的方法里再也没有使用过,所以对后面不会有影响。
另外大致看了一下,感觉 GetProcedureTokens 这个方法也可以写得更好一些。
从这个看我觉得里面有一些代码也不像想象的那么完美。说的不对的地方请各位指正。