我有一個常見的數據訪問方法,如下所列。它工作正常。但是,基於數據類型有多個if blocks
(在ExecuteNonQueryWithTextCommandType
方法中)。另外,用於準備List<CommandParameter
>的InsertLogSeverity方法中有太多冗餘代碼。數據類型的重構代碼
如何重構這個代碼?
參考文獻
- How can I easily convert DataReader to List<T>?
- Use of Generic Delegates
- Fastest method for SQL Server inserts, updates, selects
- Writing a Portable Data Access Layer
- DAL: Retrieve a DataTable using a Stored Procedure
- How to improve data access layer select method Pattern
- Return DataReader from DataLayer in Using statement
常見DAL
public class MyCommonDAL
{
public void ExecuteNonQueryWithTextCommandType(string commandText, List<CommandParameter> commandParameters)
{
string connectionString = @"Server=XXXX;Database=CostPage_Dev;User Id=hhhh;Password=xxxx";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = commandText;
command.CommandTimeout = 0;
foreach (CommandParameter parameterDetail in commandParameters)
{
if (String.Equals(parameterDetail.ParameterType, "Int"))
{
command.Parameters.AddWithValue(parameterDetail.ParameterName, Convert.ToInt32(parameterDetail.ParameterValue));
}
if (String.Equals(parameterDetail.ParameterType, "String"))
{
command.Parameters.AddWithValue(parameterDetail.ParameterName, Convert.ToString(parameterDetail.ParameterValue));
}
if (String.Equals(parameterDetail.ParameterType, "DateTime"))
{
command.Parameters.AddWithValue(parameterDetail.ParameterName, Convert.ToDateTime(parameterDetail.ParameterValue));
}
}
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
操作具體DAL
public class MyLogDAL
{
public void InsertLogSeverity(LogSeverityTypePOCO logSeverityType)
{
string commandText = @"INSERT INTO dbo.LogSeverityType (LogSeverityTypeID,Name,Description,CreatedDateTime)
VALUES (@LogSeverityTypeID,@Name,@Description,@CreatedDateTime)";
List<CommandParameter> commandParameters = new List<CommandParameter>();
CommandParameter parameter1 = new CommandParameter();
parameter1.ParameterName = "@LogSeverityTypeID";
parameter1.ParameterValue = logSeverityType.LogSeverityTypeID;
parameter1.ParameterType = "Int";
CommandParameter parameter2 = new CommandParameter();
parameter2.ParameterName = "@Name";
parameter2.ParameterValue = logSeverityType.Name;
parameter2.ParameterType = "String";
CommandParameter parameter3 = new CommandParameter();
parameter3.ParameterName = "@Description";
parameter3.ParameterValue = logSeverityType.Description;
parameter3.ParameterType = "String";
CommandParameter parameter4 = new CommandParameter();
parameter4.ParameterName = "@CreatedDateTime";
parameter4.ParameterValue = logSeverityType.CreatedDateTime;
parameter4.ParameterType = "DateTime";
commandParameters.Add(parameter1);
commandParameters.Add(parameter2);
commandParameters.Add(parameter3);
commandParameters.Add(parameter4);
MyCommonDAL dal = new MyCommonDAL();
dal.ExecuteNonQueryWithTextCommandType(commandText, commandParameters);
}
}
客戶
class Program
{
static void Main(string[] args)
{
MyLogDAL logDAL = new MyLogDAL();
LogSeverityTypePOCO logSeverityType = new LogSeverityTypePOCO();
logSeverityType.LogSeverityTypeID = 107;
logSeverityType.Name = "N";
logSeverityType.Description = "D";
logSeverityType.CreatedDateTime = DateTime.Now;
logDAL.InsertLogSeverity(logSeverityType);
}
}
DTO
public class LogSeverityTypePOCO
{
public int LogSeverityTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime CreatedDateTime { get; set; }
}
public class CommandParameter
{
public string ParameterName { get; set; }
public object ParameterValue { get; set; }
public string ParameterType { get; set; }
}
爲什麼使用自己的參數類型而不是SqlParameter? http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx –
參考文獻[寫入便攜式數據訪問層(http://msdn.microsoft .com/en-us/library/ms971568.aspx)和[DAL:使用存儲過程檢索DataTable](http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-一個-數據表-使用-A-存儲過程。aspx) – Lijo
您正在編寫專門的DAL,因爲您正在專門使用SqlConnection而不是IDbConnection –