重构数据类型的代码

时间:2013-12-19 07:49:37

标签: c# ado.net

我有一个如下所列的通用数据访问方法。它工作正常。但是,基于数据类型(在if blocks方法中)有多个ExecuteNonQueryWithTextCommandType。此外,InsertLogSeverity方法中有太多冗余代码用于准备List<CommandParameter&gt;。

如何重构此代码?

参考

  1. How can I easily convert DataReader to List<T>?
  2. Use of Generic Delegates
  3. Fastest method for SQL Server inserts, updates, selects
  4. Writing a Portable Data Access Layer
  5. DAL: Retrieve a DataTable using a Stored Procedure
  6. How to improve data access layer select method Pattern
  7. Return DataReader from DataLayer in Using statement
  8. 普通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; }
    }
    

2 个答案:

答案 0 :(得分:2)

@Lijo,根据我的评论和您的请求,我宁愿使用SqlParamter

这样的东西
public void ExecuteNonQueryWithTextCommandType(string commandText, List<SqlParameter> commandParameters)
{
    string connectionString = @"Server=XXXX;Database=CostPage_Dev;User Id=devtopco;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;
            command.Parameters.AddRange(commandParameters.ToArray());

            connection.Open();
            command.ExecuteNonQuery();
        }
    }
}

您的用法就像是

string commandText = @"INSERT INTO dbo.LogSeverityType (LogSeverityTypeID,Name,Description,CreatedDateTime) 
                  VALUES (@LogSeverityTypeID,@Name,@Description,@CreatedDateTime)";
ExecuteNonQueryWithTextCommandType(commandText, new List<SqlParameter>
{
    new SqlParameter {ParameterName = "@LogSeverityTypeID", Value = logSeverityType.LogSeverityTypeID, SqlDbType = SqlDbType.Int},
    new SqlParameter {ParameterName = "@Name", Value = logSeverityType.Name, SqlDbType = SqlDbType.VarChar},
    new SqlParameter {ParameterName = "@Description", Value = logSeverityType.Description, SqlDbType = SqlDbType.VarChar},
    new SqlParameter {ParameterName = "@CreatedDateTime", Value = logSeverityType.CreatedDateTime, SqlDbType = SqlDbType.DateTime},
});

答案 1 :(得分:1)

我认为您可以使用类型约束,例如:

    public abstract class CommandParameter
    {
        public string ParameterName { get; set; }
        public virtual object ParameterValue { get; }
        public virtual string ParameterType { get; }
    }

    public class CommandParameter<T>:CommandParameter
    {
        public T Value { get; set; }

        public override object ParameterValue
        {
            get
            {
                return Value;
            }
        }

        public override string ParameterType
        {
            get
            {
                return typeof(T).Name;
            }
        }
    }

用法:

            var param = new CommandParameter<string>();
            param.ParameterName = "@blahblah";
            param.Value = "stringVal";

            var param2 = new CommandParameter<int>();
            param2.ParameterName = "@blahInt";
            param2.Value = 123;

当您需要在更高级别引用ParameterValue时,只需将ParameterValue引用为Object类型,而不必转换它。