jeudi 26 février 2015

Result with value or error returned from service c#


First of all, let me explain the current situation. In a ASP.NET project, we do some ajax calls to a WCF service. This service always returns a value when called. But this is the way they implemented this:



public class Service: IService
{
public Result<string> IDoWhatEverIWant(string parameterExample){
try{
//1) Execute code
//2) Return a value
return Helper.CreateSuccessMessage<string>("the success value");
} catch(Exception ex){
//1) Do some stuff
//2) Return an error
return Helper.CreateErrorMessage("The error");
}
}
}

public class Result<T>{
public T ReturnedValue {get;set;}
public string Error {get;set;}
}


So they always return a "Result" instead of throwing exceptions or returning just the data.


I know this is a personal kind of taste question, but I wonder what are pro/con arguments to return a "Result" instead of just returning the value or exception?


The way I would implement is, is to just return the value if success. If an exception occurred, I would parse it, to userfriendly exception (so the users don't know to much) and throw that one and let the errorhandling from ajax catch it and convert to a correct message for the users.





Aucun commentaire:

Enregistrer un commentaire