gpt4 book ai didi

c# - 通用委托(delegate)的使用

转载 作者:太空狗 更新时间:2023-10-29 20:39:28 26 4
gpt4 key购买 nike

我们知道“Action、Func 和 Predicate 是预定义的通用委托(delegate)。因此作为委托(delegate),它们可以指向具有指定签名的函数。”

我有以下数据访问场景,其中 Func<T,R>帮助 avoiding a foreach loop在调用方法中。方法 2 没有循环。这里Func<T,R>有助于避免循环。

泛型委托(delegate)还有哪些场景可以节省大量代码?

引用文献

  1. Dynamically Composing Expression Predicates
  2. Advanced C#
  3. C#/.NET Little Wonders: The Predicate, Comparison, and Converter Generic Delegates
  4. Func vs. Action vs. Predicate
  5. What is Func, how and when is it used
  6. How can I pass in a func with a generic type parameter?

代码

方法一

public class MyCommonDAL
{

public static IEnumerable<IDataRecord> ExecuteQueryWithTextCommandType(string commandText, List<SqlParameter> commandParameters)
{
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();
using (var rdr = command.ExecuteReader())
{
while (rdr.Read())
{
yield return rdr;
}
rdr.Close();
}
}
}
}

}


public class MyLogDAL
{
public List<LogSeverityType> GetLogSeveritiesFirstApproach(LogSeverityType logSeverityType)
{


List<SqlParameter> commandParameters = new List<SqlParameter>()
{
new SqlParameter {ParameterName = "@CreatedDateTime",
Value = logSeverityType.CreatedDateTime,
SqlDbType = SqlDbType.DateTime}
};


string commandText = @"SELECT * FROM dbo.LogSeverityType WHERE CreatedDateTime > @CreatedDateTime";
var results = MyCommonDAL.ExecuteQueryWithTextCommandType(commandText, commandParameters);

List<LogSeverityType> logSeverities = new List<LogSeverityType>();

//LOOP
foreach (IDataRecord rec in results)
{
LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(rec);
logSeverities.Add(objLogSeverityType);
}


return logSeverities;
}



}

方法二

    public class MyCommonDAL
{

public static IEnumerable<T> ExecuteQueryGenericApproach<T>(string commandText, List<SqlParameter> commandParameters, Func<IDataRecord, T> factoryMethod)
{

//Action, Func and Predicate are pre-defined Generic delegates.
//So as delegate they can point to functions with specified signature.

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();
using (var rdr = command.ExecuteReader())
{
while (rdr.Read())
{
yield return factoryMethod(rdr);
}
rdr.Close();
}
}
}
}


}


public class MyLogDAL
{

public List<LogSeverityType> GetLogSeveritiesSecondApproach(LogSeverityType logSeverityType)
{


List<SqlParameter> commandParameters = new List<SqlParameter>()
{
new SqlParameter {ParameterName = "@CreatedDateTime",
Value = logSeverityType.CreatedDateTime,
SqlDbType = SqlDbType.DateTime}
};


string commandText = @"SELECT * FROM dbo.LogSeverityType WHERE CreatedDateTime > @CreatedDateTime";
//var results = MyCommonDAL.ExecuteQueryWithTextCommandType(commandText, commandParameters);

IEnumerable<LogSeverityType> logSeverities = MyCommonDAL.ExecuteQueryGenericApproach<LogSeverityType>(commandText, commandParameters, LogSeverityType.LogSeverityTypeFactory);

//foreach (IDataRecord rec in results)
//{
// LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(rec);
// logSeverities.Add(objLogSeverityType);
//}


return logSeverities.ToList();
}


}

需要其他代码

    public class LogSeverityType
{
public int LogSeverityTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime CreatedDateTime { get; set; }

public static LogSeverityType LogSeverityTypeFactory(IDataRecord record)
{
return new LogSeverityType
{
LogSeverityTypeID = (int)record[0],
Name = (string) record[1],
Description = (string)record[2],
CreatedDateTime = (DateTime) record[3]
};
}
}

static void Main(string[] args)
{
MyLogDAL logDAL = new MyLogDAL();
LogSeverityType logSeverityType = new LogSeverityType();
logSeverityType.CreatedDateTime = Convert.ToDateTime("1/1/2000");

List<LogSeverityType> logSeverities = logDAL.GetLogSeveritiesSecondApproach(logSeverityType);
}

最佳答案

我在解析/查找 XML/HTML 文档中的节点并将值分配给属性时使用通用委托(delegate)。我写了a blog post about it ,它显示重构代码以传递通用委托(delegate),以及删除了多少代码。

关于c# - 通用委托(delegate)的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20681960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com