【发布时间】:2018-10-03 05:48:46
【问题描述】:
我尝试实现一个装饰器模式来处理数据库事务中的错误。我对标准的 Func 和 Actions 没有任何问题,但我对具有 out 参数的函数有困难。
这里有很多相同问题的主题,我想办法实现我自己的委托:
public delegate TResult FuncWithOut<T1, T2, TResult>(T1 arg1, out T2 arg2);
1) 但是我没有找到如何基于这个委托实现方法:
private void SafetyExecuteMethod(Action action)
{
try
{
action();
}
catch (Exception ex)
{
// Some handling
}
}
private T SafetyExecuteFunction<T>(Func<T> func)
{
T result = default(T);
SafetyExecuteMethod(() => result = func.Invoke());
return result;
}
private SafetyExecuteFunctionWithOut // ??
{
// ??
}
2) 以及如何调用这个方法:
public bool UserExists(string name)
{
return SafetyExecuteFunction(() => _innerSession.UserExists(name));
}
public void CreateUser(string name, string password)
{
SafetyExecuteMethod(() => _innerSession.CreateUser(name, password));
}
public bool CanUpdateUser(string userName, out string errorMessage)
{
// ??
// _innerSession.CanUpdateUser(userName, out errorMessage);
}
【问题讨论】:
-
1) 究竟是什么问题?它编译吗?还有什么? 2) 三种方法。哪个?
-
1) 我不知道如何实现“SafetyExecuteFunctionWithOut”方法。 2)以及如何调用这个方法
标签: c# delegates decorator out