使用通用委托的 [英] Use of Generic Delegates
问题描述
我们知道,采取行动,Func键和predicate是pre定义泛型委托。因此,作为代表,他们可以指向与指定的签名功能。
我有以下的数据访问方案,其中 Func键< T,R>
有助于避免foreach循环
在调用方法。该方法2不具有循环。在这里, Func键< T,R>
有助于避免环路
什么是其他方案的通用名代表,它可以节省大量的code?
行参考
- 动态创作前pression predicates
- 高级C#
- <一个href="http://geekswithblogs.net/BlackRabbit$c$cr/archive/2011/11/24/c.net-little-wonders-the-$p$pdicate-comparison-and-converter-generic-delegates.aspx"相对=nofollow> C#/。NET小奇迹:在predicate,比较和转换泛型委托
- Func键与行动与predicate
- 什么是Func键,如何以及什么时候使用它
- <一个href="http://stackoverflow.com/questions/12578965/how-can-i-pass-in-a-func-with-a-generic-type-parameter">How我可以通过在FUNC了泛型类型参数?
code
办法1
公共类MyCommonDAL
{
公共静态的IEnumerable&LT; IDataRecord&GT; ExecuteQueryWithTextCommandType(字符串CommandText中,列表与LT;的SqlParameter&GT; commandParameters)
{
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
使用(SqlCommand的命令=新的SqlCommand())
{
command.Connection =连接;
command.CommandType = CommandType.Text;
command.CommandText =的CommandText;
command.CommandTimeout = 0;
command.Parameters.AddRange(commandParameters.ToArray());
connection.Open();
使用(VAR RDR = Command.ExecuteReader却())
{
而(rdr.Read())
{
得到的回报RDR;
}
rdr.Close();
}
}
}
}
}
公共类MyLogDAL
{
公开名单&LT; LogSeverityType&GT; GetLogSeveritiesFirstApproach(LogSeverityType logSeverityType)
{
名单&LT;的SqlParameter&GT; commandParameters =新的名单,其中,SqlParameter的&GT;()
{
新的SqlParameter {参数名称=@CreatedDateTime
值= logSeverityType.CreatedDateTime,
的SqlDbType = SqlDbType.DateTime}
};
字符串的CommandText = @SELECT * FROM dbo.LogSeverityType WHERE CreatedDateTime&GT; @CreatedDateTime;
VAR的结果= MyCommonDAL.ExecuteQueryWithTextCommandType(CommandText中,commandParameters);
名单&LT; LogSeverityType&GT; logSeverities =新的名单,其中,LogSeverityType&GT;();
//循环
的foreach(IDataRecord REC的结果)
{
LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(REC);
logSeverities.Add(objLogSeverityType);
}
返回logSeverities;
}
}
办法2
公共类MyCommonDAL
{
公共静态的IEnumerable&LT; T&GT; ExecuteQueryGenericApproach&LT; T&GT;(字符串CommandText中,列表与LT;的SqlParameter&GT; commandParameters,Func键&LT; IDataRecord,T&GT; factoryMethod)
{
//操作,FUNC键,predicate是pre定义泛型委托。
//因此,作为代表,他们可以指向函数指定的签名。
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
使用(SqlCommand的命令=新的SqlCommand())
{
command.Connection =连接;
command.CommandType = CommandType.Text;
command.CommandText =的CommandText;
command.CommandTimeout = 0;
command.Parameters.AddRange(commandParameters.ToArray());
connection.Open();
使用(VAR RDR = Command.ExecuteReader却())
{
而(rdr.Read())
{
收益回报factoryMethod(RDR);
}
rdr.Close();
}
}
}
}
}
公共类MyLogDAL
{
公开名单&LT; LogSeverityType&GT; GetLogSeveritiesSecondApproach(LogSeverityType logSeverityType)
{
名单&LT;的SqlParameter&GT; commandParameters =新的名单,其中,SqlParameter的&GT;()
{
新的SqlParameter {参数名称=@CreatedDateTime
值= logSeverityType.CreatedDateTime,
的SqlDbType = SqlDbType.DateTime}
};
字符串的CommandText = @SELECT * FROM dbo.LogSeverityType WHERE CreatedDateTime&GT; @CreatedDateTime;
// VAR的结果= MyCommonDAL.ExecuteQueryWithTextCommandType(CommandText中,commandParameters);
IEnumerable的&LT; LogSeverityType&GT; logSeverities = MyCommonDAL.ExecuteQueryGenericApproach&LT; LogSeverityType&GT;(CommandText中,commandParameters,LogSeverityType.LogSeverityTypeFactory);
//的foreach(IDataRecord REC的结果)
// {
// LogSeverityType objLogSeverityType = LogSeverityType.LogSeverityTypeFactory(REC);
// logSeverities.Add(objLogSeverityType);
//}
返回logSeverities.ToList();
}
}
其他$ C C必填$
公共类LogSeverityType
{
公众诠释LogSeverityTypeID {获得;组; }
公共字符串名称{;组; }
公共字符串描述{获得;组; }
公开日期时间CreatedDateTime {获得;组; }
公共静态LogSeverityType LogSeverityTypeFactory(IDataRecord记录)
{
返回新LogSeverityType
{
LogSeverityTypeID =(int)的记录[0],
名称=(字符串)记录[1],
说明=(字符串)记录[2]
CreatedDateTime =(DateTime的)记录[3]
};
}
}
静态无效的主要(字串[] args)
{
MyLogDAL logDAL =新MyLogDAL();
LogSeverityType logSeverityType =新LogSeverityType();
logSeverityType.CreatedDateTime = Convert.ToDateTime(1/1/2000);
名单&LT; LogSeverityType&GT; logSeverities = logDAL.GetLogSeveritiesSecondApproach(logSeverityType);
}
我在解析时/发现在XML / HTML文档中的节点,并在分配值属性使用泛型委托。我写它的一篇博客文章,这说明重构code到通过在一个通用的委托,多少code去除。
We know that "Action, Func and Predicate are pre-defined Generic delegates. So as delegate they can point to functions with specified signature."
I have following data-access scenario in which Func<T,R>
helps in avoiding a foreach loop
in the calling method. The approach 2 doesn’t have looping. Here Func<T,R>
helped to avoid loop.
What are the other scenarios for generic delegates in which it can save lots of lines of code?
REFERENCES
- Dynamically Composing Expression Predicates
- Advanced C#
- C#/.NET Little Wonders: The Predicate, Comparison, and Converter Generic Delegates
- Func vs. Action vs. Predicate
- What is Func, how and when is it used
- How can I pass in a func with a generic type parameter?
CODE
Approach 1
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;
}
}
Approach 2
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();
}
}
Other Code Required
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);
}
I use generic delegates when parsing / finding nodes in XML / HTML documents, and assigning the values to properties. I wrote a blog post about it, which shows refactoring the code to pass in a generic delegate, and how much code was removed.
这篇关于使用通用委托的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!