使用泛型委托
使用泛型委托
我们知道,“Action,Func和Predicate是预定义的通用委托。因此,作为委托,它们可以指向具有指定签名的函数。”\n我在以下数据访问场景中使用了Func
来帮助在调用方法中“避免使用foreach循环”。方法2没有循环。在这里,Func
帮助避免了循环。\n在哪些其他通用委托的场景中,它可以节省大量代码行?\n参考资料\n
- \n
- 动态组合表达式谓词
- 高级C#
- C#/.NET小窍门:Predicate、Comparison和Converter通用委托
- Func vs. Action vs. Predicate
- Func是什么,它如何以及何时使用
- 我如何传递一个带有通用类型参数的Func?
\n
\n
\n
\n
\n
\n
\n代码\n方法1\n
public class MyCommonDAL { public static IEnumerableExecuteQueryWithTextCommandType(string commandText, List 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 GetLogSeveritiesFirstApproach(LogSeverityType logSeverityType) { List commandParameters = new List () { 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 logSeverities = new List (); //循环 foreach (IDataRecord rec in results) { LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(rec); logSeverities.Add(objLogSeverityType); } return logSeverities; } }
\n方法2\n
public class MyCommonDAL { public static IEnumerableExecuteQueryGenericApproach (string commandText, List commandParameters, Func factoryMethod) { //Action,Func和Predicate是预定义的通用委托。 //因此,作为委托,它们可以指向具有指定签名的函数。 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 GetLogSeveritiesSecondApproach(LogSeverityType logSeverityType) { List commandParameters = new List () { 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 logSeverities = MyCommonDAL.ExecuteQueryGenericApproach (commandText, commandParameters, LogSeverityType.LogSeverityTypeFactory); //foreach (IDataRecord rec in results) //{ // LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(rec); // logSeverities.Add(objLogSeverityType); //} return logSeverities.ToList(); } }
\n其他所需的代码\n
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"); ListlogSeverities = logDAL.GetLogSeveritiesSecondApproach(logSeverityType); }
使用通用委托在解析XML / HTML文档中的节点和查找节点时,并将值分配给属性。我写了一篇关于此的博客文章,展示了通过传递通用委托来重构代码,并删除了大量代码。
在处理XML / HTML文档时,我们通常需要解析节点和查找特定节点,并将节点的值分配给属性。通常情况下,我们会编写大量的代码来处理这些操作,这样会导致代码冗余和可维护性的问题。
为了解决这个问题,我们可以使用通用委托来重构代码。通用委托是一种可以接受不同类型参数并返回相同类型结果的委托。通过使用通用委托,我们可以将节点解析和值分配的逻辑封装到一个委托方法中,然后通过传递委托方法来执行相应的操作。
在我的博客文章中,我展示了如何使用通用委托来重构代码,并展示了通过使用通用委托,可以大大减少代码量。通过将节点解析和值分配的逻辑封装到一个通用委托方法中,我们可以在不同的场景中重复使用相同的逻辑,从而减少了代码的冗余。
下面是我在博客文章中展示的使用通用委托的示例代码:
public delegate T ParserDelegate
public T ParseValue
{
return parser(input);
}
public void Example()
{
string xml = "
int value = ParseValue
string stringValue = ParseValue
Console.WriteLine("Parsed int value: " + value);
Console.WriteLine("Parsed string value: " + stringValue);
}
通过以上示例代码,我们可以看到,通过将解析逻辑封装到通用委托方法中,并使用通用委托方法来执行解析操作,我们可以在不同的场景中重用相同的解析逻辑,从而减少了代码量。
总之,通过使用通用委托,我们可以将解析和值分配的逻辑封装到一个方法中,并在需要时传递该方法来执行相应的操作。这样可以大大减少代码量,并提高代码的可维护性和重用性。