I like to use this approach:
[DataContract]
public class OperationResult
{
public OperationResult()
{
Errors = new List<OperationError>();
Success = true;
}
[DataMember]
public bool Success { get; set; }
[DataMember]
public IList<OperationError> Errors { get; set; }
}
[DataContract(Name = "OperationResultOf{0}")]
public class OperationResult<T> : OperationResult
{
[DataMember]
public T Result { get; set; }
}
[DataContract]
public class OperationError
{
[DataMember]
public string ErrorCode { get; set; }
[DataMember]
public string ErrorMessage { get; set; }
}
Also I have some extensions:
public static OperationResult WithError(this OperationResult operationResult, string errorCode,
string error = null)
{
return operationResult.AddErrorImpl(errorCode, error);
}
public static OperationResult<T> WithError<T>(this OperationResult<T> operationResult, string errorCode,
string error = null)
{
return (OperationResult<T>) operationResult.AddErrorImpl(errorCode, error);
}
private static OperationResult AddErrorImpl(this OperationResult operationResult, string errorCode,
string error = null)
{
var operationError = new OperationError {Error = error ?? string.Empty, ErrorCode = errorCode};
operationResult.Errors.Add(operationError);
operationResult.Success = false;
return operationResult;
}
public static OperationResult<T> WithResult<T>(this OperationResult<T> operationResult, T result)
{
operationResult.Result = result;
return operationResult;
}
The extensions make possible to return error with the one line of code:
return retValue.WithError(ErrorCodes.RequestError);
From my wcf service I never throw an exception.
Sorry for the wall of code
Caller's code is something like that. But it all depends on your requirements
OperationResult res = _service.Register(username, password);
if(!res.Success)
{
if(res.Errors.Any(x => ErrorCodes.UsernameTaken)
{
// show error for taken username
}
...
}