I know Result patterns can be a bit controversial as far as their benefits, but I'm giving them a try in my project. Anyways...I've outsourced some work to a scoped service which returns a Result<T>. My methods which call this outsourced code have this exact same structure:
var result = await service.DoSomethingAsync(...);if (!result.IsSuccess) return Response(result.ErrorMessage);var obj = result.Value;// do stuff with obj
var result = await service.DoSomethingAsync(...);if (!result.IsSuccess) return Response(result.ErrorMessage);var obj = result.Value;// do stuff with obj
Are there any ways to reduce this into a simpler form to further reduce the amount of code needed to perform the "try action, return error message if error else get success value" process? I certainly will concede I think I'm splitting hairs here and this is already simple enough for some, but I feel like I'm...missing something to make this more streamlined - maybe pattern matching could be utilized here?