问题描述
我在课堂上有两个职能.唯一的区别是函数的参数(一个带有X的Func,另一个带有Y的Func)以及标有星号的行.
I have two functions in a class. The only differences are the parameter to the function (one taking a Func with a X and the other a Y) and the lines marked with asterisks.
是否有任何方法可以用星号分隔这两行或具有一个通用功能,或者重写这些功能以使try,catch块和最后几条语句仅被写入一次?
Is there any way to isolate those two lines with asterisks or have a common function, or rewrite the functions such that the try, catch block and last few statements are written only once?
这里的目标是最大程度地减少代码重复.
The objective here is to minimize code duplication.
public T Do<T>(Func<X, T> something)
{
try
{
var manager = CoreInfrastructure.GetManager(Prefix, Config.Param1, Config.Param2); //******
if (manager != null) return something(manager);
LoggingHandler.LogWarning(LogTitle, $"manager ({this}) is null.");
}
catch (MyException exp)
{
ExceptionHandler.HandleRecoverableException(exp, LogTitle,
$"query on manager ({this}) failed.");
}
var msg = $"failed to query using manager ({this})!";
LoggingHandler.LogCritical(LogTitle, msg);
throw new MyException(msg);
}
public T Do<T>(Func<Y, T> something)
{
try
{
var manager = CoreInfrastructure.GetManager(Prefix, Config.Param3); //******
if (manager != null) return something(manager);
LoggingHandler.LogWarning(LogTitle, $"manager ({this}) is null.");
}
catch (MyException exp)
{
ExceptionHandler.HandleRecoverableException(exp, LogTitle,
$"query on manager ({this}) failed.");
}
var msg = $"failed to query using manager ({this})!";
LoggingHandler.LogCritical(LogTitle, msg);
throw new MyException(msg);
}
推荐答案
创建接受管理器和函数的通用方法
Create generic method that accepts manager and function
public T Do<T, TManager>(TManager manager, Func<TManager, T> something)
{
try
{
if (manager != null) return something(manager);
LoggingHandler.LogWarning(LogTitle, $"manager ({this}) is null.");
}
catch (MyException exp)
{
ExceptionHandler.HandleRecoverableException(exp, LogTitle,
$"query on manager ({this}) failed.");
}
var msg = $"failed to query using manager ({this})!";
LoggingHandler.LogCritical(LogTitle, msg);
throw new MyException(msg);
}
public void DoAll<T>(Func<X, T> somethingX, Func<Y, T> somethingY)
{
Do(CoreInfrastructure.GetManager(Prefix, Config.Param1, Config.Param2), somethingX);
Do(CoreInfrastructure.GetManager(Prefix, Config.Param3), somethingY);
}
如Damien_The_Unbeliever所述,如果创建管理器可以是 MyException
的来源,则可以添加 Func< TManager>
而不是TManager管理器:
As Damien_The_Unbeliever mentioned if creating manager can be source of MyException
you can add Func<TManager>
instead of TManager manager:
public T Do<T, TManager>(Func<TManager> managerCreate, Func<TManager, T> something)
{
try
{
TManager manager = managerCreate();
if (manager != null) return something(manager);
LoggingHandler.LogWarning(LogTitle, $"manager ({this}) is null.");
}
catch (MyException exp)
{
ExceptionHandler.HandleRecoverableException(exp, LogTitle,
$"query on manager ({this}) failed.");
}
var msg = $"failed to query using manager ({this})!";
LoggingHandler.LogCritical(LogTitle, msg);
throw new MyException(msg);
}
public void DoAll<T>(Func<X, T> somethingX, Func<Y, T> somethingY)
{
Do(() => CoreInfrastructure.GetManager(Prefix, Config.Param1, Config.Param2), somethingX);
Do(() => CoreInfrastructure.GetManager(Prefix, Config.Param3), somethingY);
}
您可以更进一步,并用参数化函数替换 Func< TManager>
,并在其中传递 Prefix
和 Config
You can go further and replace Func<TManager>
with parametrized function where you pass Prefix
and Config
这篇关于跨通用函数隔离通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!