我的所有的东西我一直在试图做了一会儿,还没有找到一个很好的策略,这样做,我不知道C#中甚至可以支持什么,我试图做的。
为例想像这样的一个模板,在经理code总体cocept功能重复返回一个结果由一个成功标志和错误列表。
公开结果<布尔> RemoveLocation(LocationKey键)
{
名单<错误> errorList =新的名单,其中,错误>();
布尔结果= NULL;
尝试{
结果= locationDAO.RemoveLocation(密钥);
}赶上(UpdateException UE){
//错误发生较少通过这个返回给用户!
errorList = ue.ErrorList;
}
返回新的结果<布尔>(结果,errorList);
}
展望把它变成像下面的地方做东西的模板是一些调用(preferably不是静态),返回一个布尔值。我知道我可以在一个堆栈意义上做到这一点,但我确实在寻找一种方式,通过对象的引用来做到这一点。
公开结果<布尔> RemoveLocation(LocationKey键)
{
VAR魔术= locationDAO.RemoveLocation(密钥);
返回ProtectedDAOCall(魔法);
}
公开结果<布尔> CreateLocation(LocationKey键)
{
VAR魔术= locationDAO.CreateLocation(密钥);
返回ProtectedDAOCall(魔法);
}
公开结果<布尔> ProtectedDAOCall(Func键< ...,布尔>的doSomething)
{
名单<错误> errorList =新的名单,其中,错误>();
布尔结果= NULL;
尝试{
结果= DoSomething的();
}赶上(UpdateException UE){
//错误发生较少通过这个返回给用户!
errorList = ue.ErrorList;
}
返回新的结果<布尔>(结果,errorList);
}
如果有任何你可能需要让我知道更多的信息。
我想看看别人能拿出。
马克的解决方案适用于code以上
公开结果<布尔> CreateLocation(LocationKey键)
{
LocationDAO locationDAO =新LocationDAO();
返回WrapMethod(()=> locationDAO.CreateLocation(键));
}
公开结果<布尔> RemoveLocation(LocationKey键)
{
LocationDAO locationDAO =新LocationDAO();
返回WrapMethod(()=> locationDAO.RemoveLocation(键));
}
静态结果< T> WrapMethod< T>(Func键<结果< T>> FUNC)
{
尝试
{
返回FUNC();
}
赶上(UpdateException UE)
{
返回新的结果< T>(默认值(T),ue.Errors);
}
}
解决方案
是这样的:
公开结果<布尔> RemoveLocation(LocationKey键)
{
返回WrapMethod(()=> locationDAO.RemoveLocation(键));
}
静态结果< T> WrapMethod< T>(Func键< T> FUNC){
尝试
{
返回新的结果< T>(FUNC());
}
赶上(SomeExceptionBase前)
{
返回新的结果< T>(ex.ErrorList);
}
赶上(例外前)
{
返回新的结果< T>((名单<错误>)NULL);
}
}
和(显示最小)
类结果< T>
{
民营结果(布尔ISERROR,T值,列表和LT;错误> erors){}
公开结果(T值):这个(假的,值null){}
公开结果(表<错误>错误):这个(真,默认(T),错误){}
}
类SomeExceptionBase:异常
{
公开名单<错误> ErrorList {获得;私定; }
}
(尽管如果我的有的做到这一点我可能会做一些与异常的更多有趣的是,不正好是 SomeExceptionBase
)
All i have something i have been trying to do for a while and have yet to find a good strategy to do it, i am not sure C# can even support what i am trying to do.
Example imagine a template like this, repeated in manager code overarching cocept function Returns a result consisting of a success flag and error list.
public Result<Boolean> RemoveLocation(LocationKey key)
{
List<Error> errorList = new List<Error>();
Boolean result = null;
try{
result = locationDAO.RemoveLocation(key);
}catch(UpdateException ue){
//Error happened less pass this back to the user!
errorList = ue.ErrorList;
}
return new Result<Boolean>(result, errorList);
}
Looking to turn it into a template like the below where Do Something is some call (preferably not static) that returns a Boolean. I know i could do this in a stack sense, but i am really looking for a way to do it via object reference.
public Result<Boolean> RemoveLocation(LocationKey key)
{
var magic = locationDAO.RemoveLocation(key);
return ProtectedDAOCall(magic);
}
public Result<Boolean> CreateLocation(LocationKey key)
{
var magic = locationDAO.CreateLocation(key);
return ProtectedDAOCall(magic);
}
public Result<Boolean> ProtectedDAOCall(Func<..., bool> doSomething)
{
List<Error> errorList = new List<Error>();
Boolean result = null;
try{
result = doSomething();
}catch(UpdateException ue){
//Error happened less pass this back to the user!
errorList = ue.ErrorList;
}
return new Result<Boolean>(result, errorList);
}
If there is any more information you may need let me know.
I am interested to see what someone else can come up with.
Marc solution applied to the code above
public Result<Boolean> CreateLocation(LocationKey key)
{
LocationDAO locationDAO = new LocationDAO();
return WrapMethod(() => locationDAO.CreateLocation(key));
}
public Result<Boolean> RemoveLocation(LocationKey key)
{
LocationDAO locationDAO = new LocationDAO();
return WrapMethod(() => locationDAO.RemoveLocation(key));
}
static Result<T> WrapMethod<T>(Func<Result<T>> func)
{
try
{
return func();
}
catch (UpdateException ue)
{
return new Result<T>(default(T), ue.Errors);
}
}
解决方案
Something like:
public Result<Boolean> RemoveLocation(LocationKey key)
{
return WrapMethod(() => locationDAO.RemoveLocation(key));
}
static Result<T> WrapMethod<T>(Func<T> func) {
try
{
return new Result<T>(func());
}
catch (SomeExceptionBase ex)
{
return new Result<T>(ex.ErrorList);
}
catch (Exception ex)
{
return new Result<T>((List<Error>)null);
}
}
and (minimum shown)
class Result<T>
{
private Result(bool isError, T value, List<Error> erors) { }
public Result(T value) : this(false, value, null){ }
public Result(List<Error> errors) : this(true, default(T), errors) { }
}
class SomeExceptionBase : Exception
{
public List<Error> ErrorList { get; private set; }
}
(although if I had to do this I'd probably do something more interesting with exceptions that don't happen to be SomeExceptionBase
)