Skip to content

Instantly share code, notes, and snippets.

@pedrovasconcellos
Created November 30, 2018 20:02
Show Gist options
  • Save pedrovasconcellos/25b15edcfb604f4e8a1e2e0f2ac7ef41 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/25b15edcfb604f4e8a1e2e0f2ac7ef41 to your computer and use it in GitHub Desktop.
Result<T>
using System.Collections.Generic;
using System.Linq;
public class Result<T>
{
public Result()
{
this.Errors = new Dictionary<string, string[]>();
}
public Result(T response)
{
this.Response = response;
this.Errors = new Dictionary<string, string[]>();
}
public Result(Dictionary<string, string[]> errors)
{
this.Errors = errors;
}
public Result(T response, Dictionary<string, string[]> errors)
{
this.Response = response;
this.Errors = errors;
}
public Result(T response, string parameterError, string error)
{
this.Response = response;
this.Errors = new Dictionary<string, string[]>
{
{ parameterError, new string[] { error } }
};
}
public T Response { get; private set; }
public Dictionary<string, string[]> Errors { get; private set; }
public Result<T> AddError(string parameter, string error)
{
Errors.Add(parameter, new string[] { error });
return this;
}
public Result<T> AddError(string parameter, string[] errors)
{
Errors.Add(parameter, errors);
return this;
}
public bool ContainErrorDescription(string errorDescription)
{
return this.Errors.Values.Contains(new string[] { errorDescription });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment