[英]Pass logic function as parameter to general function
Trying to reduse some code and follow the DRY principle with a generic approach. 尝试重新使用一些代码,并采用通用方法遵循DRY原则。 The code is kind of trivial, but I can not manage to compile it. 该代码有点琐碎,但我无法对其进行编译。 I guess you see what I'm trying to do here (not repeat GetData(), but rather have several Logic() func's), what am I doing wrong? 我想您会看到我在这里尝试做的事情(不是重复GetData(),而是有几个Logic()函数),我在做什么错?
Caller: 呼叫者:
IEnumerable<IMyClass> result = Factory.GetData<IEnumerable<IMyClass>>("storedProsedureName", Factory.LogicFunction());
Logic: 逻辑:
public static IEnumerable<T> GetData<T>(string storedProsedureName, Func<string, OracleConnection, OracleCommand, IEnumerable<T>> functionCall) where T : class
{
using (OracleConnection conn = new OracleConnection(Helpers.GetConnectionString()))
{
using (OracleCommand cmd = conn.CreateCommand())
{
try
{
return functionCall(storedProsedureName, conn, cmd);
}
catch (Exception e)
{
throw e;
}
}
}
}
public static IEnumerable<IMyClass> LogicFunction(string storedProsedureName= "", OracleConnection conn = null, OracleCommand cmd = null)
{
conn.Open();
{...}
conn.Close();
return IEnumerable<IMyClass>;
}
Error: 错误:
Argument 2: cannot convert 参数2:无法转换
from 从
'System.Collections.Generic.IEnumerable_IMyClass>' 'System.Collections.Generic.IEnumerable_IMyClass>'
to 至
'System.Func_string, OracleConnection, OracleCommand, IEnumerable _IEnumerable_IMyClass>>>' 'System.Func_string,OracleConnection,OracleCommand,IEnumerable _IEnumerable_IMyClass >>>'
Change your caller to below code. 将您的呼叫者更改为以下代码。
Removed ()
and also changed generic closing type from IEnumerable<IMyClass>
to IMyClass
. 删除了()
,并将通用关闭类型从IEnumerable<IMyClass>
更改为IMyClass
。
IEnumerable<IMyClass> result = Factory.GetData<IMyClass>("storedProsedureName", Factory.LogicFunction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.