Sunday, April 28, 2019

Better way to accept Func parameter than IActionResult SafeExec(T param, Func operation)

I have this method in the base class

protected IActionResult SafeExec<T>(T param, Func<T, IActionResult> operation) {

try {

return operation.Invoke(param);

}

catch (Exception ex) {

if (ex.IsCritical()) {

throw;

}

Logger.Error(ex);

return StatusCode((int)HttpStatusCode.InternalServerError, ex.ToStringVerbose());

}

}

Calling it is a little awkward

SafeExec(buildHierarchy, (_buildHierarchy) => {

...

return Ok(helper.GetJochierarchy(items.ToList(), _buildHierarchy));

});

Is there a way I can write that so calling it can be done with just a lambda type function syntax. Like this:<code>

SafeExec(buildHierarchy => {

...

return Ok(helper.GetJochierarchy(items.ToList(), buildHierarchy));

});

Better way to accept Func parameter than IActionResult SafeExec<T>(T param, Func<T, IActionResult> operation) Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team