Created
May 25, 2022 02:40
-
-
Save detroitpro/6362a936074feb517b7c0d4c53afbe9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
namespace Core | |
{ | |
public class Result | |
{ | |
public bool Ok { get; set; } | |
public string Message { get; set; } | |
public static Result Error(string error) | |
{ | |
Console.WriteLine($"Result Error {error}"); | |
return new Result() | |
{ | |
Ok = false, | |
Message = error, | |
}; | |
} | |
public static Result Success(string message = "success") | |
{ | |
Console.WriteLine(message); | |
return new Result() | |
{ | |
Ok = true, | |
Message = message | |
}; | |
} | |
public override string ToString() | |
{ | |
return JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); | |
} | |
} | |
public class Result<T> : Result | |
{ | |
public T Data { get; set; } | |
public static Result<T> Success(T data, string message = "success") | |
{ | |
return new Result<T>() | |
{ | |
Ok = true, | |
Data = data, | |
Message = message | |
}; | |
} | |
public static new Result<T> Error(string error) | |
{ | |
Console.WriteLine($"Result Error {error}"); | |
return new Result<T>() | |
{ | |
Ok = false, | |
Message = error, | |
}; | |
} | |
public static Result<T> Error(T data, string error) | |
{ | |
Console.WriteLine($"Result Error {error}"); | |
return new Result<T>() | |
{ | |
Ok = false, | |
Message = error, | |
}; | |
} | |
} | |
public class Result<T, TT> : Result | |
{ | |
public T Data { get; set; } | |
public TT ErrorObject { get; set; } | |
public static Result<T, TT> Success(T data, string message = "success") | |
{ | |
return new Result<T, TT>() | |
{ | |
Ok = true, | |
Data = data, | |
Message = message | |
}; | |
} | |
public static new Result<T, TT> Error(string error) | |
{ | |
Console.WriteLine($"Result Error {error}"); | |
return new Result<T, TT>() | |
{ | |
Ok = false, | |
Message = error, | |
}; | |
} | |
public static Result<T, TT> Error(T data, string error) | |
{ | |
Console.WriteLine($"Result Error {error}"); | |
return new Result<T, TT>() | |
{ | |
Ok = false, | |
Message = error, | |
}; | |
} | |
public static Result<T, TT> Error(TT errorObject) | |
{ | |
return new Result<T, TT>() | |
{ | |
Ok = false, | |
ErrorObject = errorObject, | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment