Created
January 30, 2024 03:32
-
-
Save nickpreston24/46332455f0d23667f74ee2e9ca0135ee to your computer and use it in GitHub Desktop.
Please for the love of all that is dotnet, let's do Less Null Checking!
This file contains 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
namespace Extensions.Types; | |
public static class TypeExtensions | |
{ | |
/* | |
* | |
* String to Type Converters | |
* | |
* WHY? Because converting VB to C# and handling null strings with a bunch of `if` statement is a waste of your time and mine... | |
* How? Ternary logic and fallbacks (or default) | |
* | |
* Feel free to use and chain everything to your hearts content. | |
*/ | |
public static int ToInt(this string text, int fallback = default) | |
{ | |
return int.TryParse(text, out int result) ? result : fallback; | |
} | |
public static short ToShort(this string text, short fallback = default) | |
{ | |
return short.TryParse(text, out short result) ? result : fallback; | |
} | |
public static double ToDouble(this string text, double fallback = default) | |
{ | |
return double.TryParse(text, out double result) ? result : fallback; | |
} | |
public static decimal ToDecimal(this string text, decimal fallback = default) | |
{ | |
return decimal.TryParse(text, out decimal result) ? result : fallback; | |
} | |
public static long ToLong(this string text, long fallback = default) | |
{ | |
return long.TryParse(text, out long result) ? result : fallback; | |
} | |
public static float ToFloat(this string text, float fallback = default) | |
{ | |
return float.TryParse(text, out float result) ? result : fallback; | |
} | |
public static DateTime? ToDateTime(this string dtString, DateTime? fallback = null) | |
{ | |
if (fallback == null || !fallback.HasValue) | |
fallback = DateConstants.Epoch; | |
return DateTime.TryParse(dtString, out DateTime result) ? result : fallback; | |
} | |
public static int ToBit(this string text, int fallback = 0) | |
{ | |
return text.ToInt() == 1 ? 1 : fallback; | |
} | |
public static bool ToBoolean(this string text, bool fallback = default) | |
{ | |
return bool.TryParse(text, out bool result) ? result : fallback; | |
} | |
/** | |
* | |
* MODIFIERS | |
* | |
*/ | |
/// <summary> | |
/// Update properties on a long, nested object easily. | |
/// | |
/// ... Why though? Mutate any object just like the Visual Basic days, except (BONUS), this one is chainable! <3 | |
/// | |
/// Usage: | |
/// your.ultralarge.complex.infragistics.winforms.grid.table.row.With(row=> { | |
/// row.Text = "hello there!" | |
/// row.Color = "blue" | |
/// ... | |
/// }; | |
///</summary> | |
public static T With<T>(this T obj, Action<T> patch) | |
{ | |
patch(obj); | |
return obj; | |
} | |
/// <summary> | |
/// Map a Source class to a Target | |
/// except now it's between two different classes | |
/// Why? - Don't need AutoMapper if you'r data flow is transactional! Why add work? | |
/// </summary> | |
public static TResult Map<TSource, TResult>( | |
this TSource source, | |
Func<TSource, TResult> map | |
) => map(source); | |
} | |
public class DateConstants | |
{ | |
public static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment