A tiny helper for writing .NET Core applications. This library provides a simple set of extension methods that you can use to perform common validations and conversions in C#.
- Download and Install the latest
HelpMate.Corefrom NuGet using Package Manager or via CLI:
PM> Install-Package HelpMate.Core -Version 2.1.0
- Declare the following namespace in the class:
usingHelpMate.Core.Extensions;Assuming you would want to convert a string value to a DateTime object type. To handle this, you would probably use one of the built-in methods in .NET such as the Convert.ToDateTime() method.
stringdateString="6/10/2020";vardate=Convert.ToDateTime(dateString);That should work perfectly as we expect, however that will throw a System.FormatException if you are trying to convert a string value which is not a valid date format. This could potentially happen especially when our code is consuming data from user inputs or other data sources that we never control. To fix that, we can use the DateTime.TryParse() method to safely handle the conversion as shown in the following:
stringdateString="60/1011/2020";// this is an invalid datevarisValid=DateTime.TryParse(dateString,outvardate);if(isValid){// date is valid}You can see that our code is now safe to handle DateTime conversion, however, the code is kinda long to just do a basic conversion. Imagine you have a lot of the same conversion cluttering within the different areas of your project. That could be messy and may cost you a bit of development time. To avoid that, you would end up creating some helper functions that do common tasks such as conversions.
With HelpMate.Core, you don't need to write helper functions yourself anymore every time you implement a new project that does common things, not unless if necessary. Instead, you can just plug this package into your project and then you're good to go.
Here's a sample usage on how to do DateTime conversion easily:
stringdateString="60/1011/2020";// this is an invalid datevardate=dateString.ToDateTime();if(date.IsValidDateTime()){// Date is valid}As you could probably guessed, the implementation in the preceding code is somewhat similar to DateTime.TryParse(). Yes it is and in fact, it uses the DateTime.TryParse() method under the hood. The goal is to provide convenience and to make your code much cleaner and shorter.
Here are the list of available methods as of version 2.x.
DateTimeToDateTime(thisstringvalue)DateTime?ToNullableDateTime(thisstringvalue)shortToInt16(thisstringvalue)short?ToNullableInt16(thisstringvalue)intToInt32(thisstringvalue)int?ToNullableInt32(thisstringvalue)longToInt64(thisstringvalue)long?ToNullableInt64(thisstringvalue)byteToByte(thisstringvalue)byte?ToNullableByte(thisstringvalue)boolToBoolean(thisstringvalue)bool?ToNullableBoolean(thisstringvalue)floatToFloat(thisstringvalue)float?ToNullableFloat(thisstringvalue)decimalToDecimal(thisstringvalue)decimal?ToNullableDecimal(thisstringvalue)doubleToDouble(thisstringvalue)double?ToNullableDouble(thisstringvalue)GuidToGuid(thisstringvalue)Guid?ToNullableGuid(thisstringvalue)stringToBase64Encode(thisstringvalue)stringToBase64Encode(thisbyte[]value)stringToBase64Decode(thisstringvalue)byte[]ToByteFromBase64CharArray(thisstringvalue)byte[]ToByteArray(thisstringvalue)stringToDateTimeFormat(thisstringdate,stringformat)stringToDateTimeFormat(thisstringdate,stringformat,CultureInfocultureInfo)TToEnum<T>(thisstringvalue,TdefaultValue,boolignoreCase=false)stringToCamelCase(thisstringvalue)stringToJson<T>(thisTvalue,JsonSerializerOptionsjsonOptions=null)intGetYearsFromDate(thisDateTimedate)intGetYearsDifference(thisDateTimedate,DateTimedateToCompare)
Format Validation ExtensionsboolIsValidEmailFormat(thisstringvalue)boolIsValidCreditCardNumber(thisstringvalue)boolIsValidPhoneNumber(thisstringvalue,intminLength=default,intmaxLength=default)boolIsValidJson(thisstringjson)boolIsValidDateTime(thisDateTime?date)boolIsValidDateTime(thisDateTimedate)boolIsValidDateTimeString(thisstringdate)boolIsValidStandardDateString(thisstringvalue)boolIsFutureDate(thisstringdate)boolIsNumber(thisstringvalue)boolIsWholeNumber(thisstringvalue)boolIsDecimalNumber(thisstringvalue)boolIsBoolean(thisstringvalue)boolIsHtml(thisstringvalue)boolIsAlphaNumeric(thisstringvalue)boolIsAlphaNumericStrict(thisstringvalue)
Null Handling ExtensionsIEnumerable<T>AsNotNull<T>(thisIEnumerable<T>source)boolIsNotNull<T>(thisIEnumerable<T>source)boolIsNotNull<T>(thisTsource)boolIsNull<T>(thisIEnumerable<T>source)boolIsNull<T>(thisTsource)ValueValidator.ThrowsWhen.Null(Tinput,stringparameterName);ValueValidator.ThrowsWhen.Null(Tinput,stringparameterName,stringmessage);ValueValidator.ThrowsWhen.NullOrWhiteSpace(stringinput,stringparameterName);ValueValidator.ThrowsWhen.NullOrEmpty(Guid?input,stringparameterName);ValueValidator.ThrowsWhen.False(boolinput,stringmesssage);ValueValidator.ThrowsWhen.True(boolinput,stringmesssage);ValueValidator.ThrowsWhen.NotDefined(objectinput);Check out the following link for sample usage:
See release logs here: Release Logs
Feel free to submit a ticket if you find bugs or request a new feature. Your valuable feedback is much appreciated to better improve this library. If you find this useful, please give it a star to show your support for this project.
Icon made by Freepik from www.flaticon.com
