Craig's Utility Library
Contains more utilities than Batman's belt
Project maintained by JaCraig
Hosted on GitHub Pages — Theme by mattgraham
I'm Batman
Have you ever thought about why Batman is as effective as he is? I mean sure he's rich, in better shape than any of us will ever be, and is a master at martial arts, but what almost always wins the day for him is that utility belt. It's the never ending supply of gadgets and utilities that he can use in the appropriate situation that really saves the day. And Craig's Utility Library is the same for programmers.
With .Net we have a number of built in classes and functions to help make a programmer's life easier, but let's face facts, they didn't think of everything. Craig's Utility Library tries to fill in some of those gaps (or at least the ones that I've run into). It comes with a couple hundred extension methods, built in data types such as a BTree, priority queue, ring buffer, etc. And that's just the DataTypes namespace. When you add it all up, Craig's Utility Library is one of the largest set of utilities for .Net out there.
C'mon, Deckard. Show Me What You're Made Of.
So let's look at a couple of examples of where these might come in handy. Have you ever been trying to download a web page?
string Content=new Uri("http://www.duckduckgo.com").Read();
That's it. Well that and adding the Utilities.IO.ExtensionMethods namespace. Want to format a string as a phone number?
string PhoneNumber="5555551010".FormatString("(###)###-####"); //Becomes (555)555-1010
Want to remove all items from an ICollection that are greater than 5?
ICollection<int> Values=new int[]{1,2,3,4,5,6,7,8,9,10}.Remove(x=>x>5); //Will return 1,2,3,4,5
Want to load every DLL from a given directory and then search it for any class that uses a specific interface and create an instance of it?
IEnumerable<IMyInterface> Classes=new DirectoryInfo("./MyAssemblies/").GetObjects<IMyInterface>();
Want to change an image into ASCII art?
using(Bitmap MyImage=new Bitmap("FileToLoad"))
{
string ASCIIArt=MyImage.ToASCIIArt();
}
And that's just a couple examples for the extension methods. Want to connect to a database but hate ADO.Net? Use SQLHelper:
using(SQLHelper Helper=new SQLHelper("SELECT * FROM MyTable",System.Data.CommandType.Text,"DatabaseConnection"))
{
Helper.ExecuteReader();
while(Helper.Read())
{
int Value1=Helper.GetParameter("Value1",0);
string Value2=Helper.GetParameter("Value2","");
}
}
This utility library contains code for caching, compression, configuration, data mapping (think AutoMapper), encryption, IO, an IoC container, LDAP/AD querying, Math helpers, image manipulation, a micro ORM (somewhere between BLToolkit and PetaPoco in terms of speed), a full blown ORM, a profiler, randomization helpers, extension methods to help with reflection, a set of classes that allow you to do AOP, DataAnnotations additions for validation, a number of file formats, as well as web related helpers.
That's Not a Knife, This is a Knife
Still don't think that it's a lot? Here's just a small list of the extension methods:
Utilities.DataTypes.ExtensionMethods
- Array extensions
- DateTime extensions
- DaysInMonth
- DaysLeftInMonth
- DaysLeftInYear
- FirstDayOfMonth
- FirstDayOfWeek
- FromUnixTime (actually extension for int and long values)
- IsInFuture
- IsInPast
- IsWeekDay
- IsWeekEnd
- LastDayOfMonth
- LastDayOfWeek
- ToUnix
- FirstDayOfQuarter
- FirstDayOfYear
- LastDayOfQuarter
- LastDayOfYear
- ConvertToTimeZone (assumes that times are in UTC time)
- LocalTimeZone
- AddWeeks
- Age
- BeginningOfDay
- EndOfDay
- IsToday
- SetTime
- UTCOffset
- Generic object extensions
- If
- NotIf
- Return
- Chain
- Do
- Execute
- ThrowIfTrue
- ThrowIfFalse
- ICollection extensions
- AddAndReturn
- AddRange
- AddIf
- AddIfUnique
- Remove
- RemoveRange
- IComparable extensions
- IDictionary extensions
- IEnumerable extensions
- Exists
- For
- ForEach
- ForParallel
- ForEachParallel
- IsNullOrEmpty
- RemoveDefaults
- ToArray
- ToString
- TrueForAll
- TryAll
- TryAllParallel
- FalseForAll
- FalseForAny
- TrueForAll
- TrueForAny
- ThrowIfFalseForAll
- ThrowIfFalseForAny
- ThrowIfTrueForAll
- ThrowIfTrueForAny
- ElementsBetween
- First
- Last
- ToDataTable
- ToCSV
- ToDelimitedFile
Keep Going...
- MatchCollection extensions
- string extensions
- Encode
- FromBase64
- Left
- Right
- ToBase64
- ToByteArray
- ToFirstCharacterUpperCase
- ToSentenceCapitalize
- ToTitleCase
- NumberTimesOccurs
- Reverse
- FilterOutText
- KeepFilterText
- AlphaNumericOnly
- AlphaCharactersOnly
- NumericOnly
- IsUnicode
- FormatString
- RegexFormat
- ExpandTabs
- StripLeft
- StripRight
- Pluralize
- Singularize
- Center
- MaskLeft
- MaskRight
- UrlDecode
- UrlEncode
- Various type conversion/type checking extensions
- FormatToString
- IsNotDefault
- IsDefault
- IsNotNull
- IsNull
- IsNotNullOrDBNull
- IsNullOrDBNull
- NullCheck
- ThrowIfDefault
- ThrowIfNullOrEmpty
- ThrowIfNullOrDBNull
- ToSQLDbType
- ToDbType
- ToType
- TryTo
Yeah, there's more...
- Various value type extensions
- ToBool (int)
- ToInt (bool)
- ToBase64String (byte array)
- ToEncodedString (byte array)
- IsUnicode (byte array)
- TimeSpan Extensions
- Years
- Months
- DaysRemainder
- DataTable Extensions
- ToList
- ToCSV
- ToDelimitedFile
Utilities.Math.ExtensionMethods
- Math extensions
- Between
- Clamp
- Factorial
- Max
- Median
- Min
- Mode
- Pow
- Round
- StandardDeviation
- Sqrt
- Variance
- Permute
OK, now we're done... With the first half of the list...
Utilities.SQL.ExtensionMethods
- DbCommand extensions
- AddParameter
- BeginTransaction
- ClearParameters
- Close
- Commit
- ExecuteDataSet
- ExecuteScalar
- GetOutputParameter
- Open
- Rollback
- DbDataReader extensions
Utilities.Compression.ExtensionMethods
- Compress (both byte arrays and strings)
- Decompress (both byte arrays and strings)
Utilities.Encryption.ExtensionMethods
- Hash (Now handles all hash algorithms in one function for both byte arrays and strings)
- Encrypt (Handles any symmetric encryption algorithm inside .Net)
- Decrypt (Handles any symmetric encryption algorithm inside .Net)
Utilities.IO.ExtensionMethods
- DirectoryInfo extensions
- CopyTo
- DeleteAll
- DeleteFiles
- DeleteFilesNewerThan
- DeleteFilesOlderThan
- Size
- SetAttribute
- DriveInfo
- DeleteDirectoriesNewerThan
- DeleteDirectoriesOlderThan
Want to cry yet?
- FileInfo extensions
- Append
- CompareTo
- Read
- ReadBinary
- Save
- SaveAsync
- SetAttributes
- DriveInfo
- Execute
- String extensions
- RemoveIllegalDirectoryNameCharacters
- RemoveIllegalFileNameCharacters
- Serialization extensions
- ToBinary
- ToJSON
- ToSOAP
- ToXML
- ToObject
- JSONToObject
- SOAPToObject
- XMLToObject
- Uri extensions
Utilities.Web.ExtensionMethods
- Web related extensions
- AbsoluteRoot
- AddScriptFile
- ContainsHTML
- HTTPCompress
- IsEncodingAccepted
- RelativeRoot
- RemoveURLIllegalCharacters
- SetEncoding
- StripHTML
- IPAddress extensions
- Minification
- Combine (can be used for HTML,JavaScript, or CSS)
- Minify (can be used for HTML, JavaScript, or CSS)
- HttpRequest extensions
Utilities.Image.ExtensionMethods
- All Bitmap functions were moved
- Added ToBase64 extension method
- Added DrawRoundedRectangle extension
- Screen extensions
Just a bit further
Utilities.Error.ExtensionMethods
- Various error related extensions
- DumpApplicationState
- DumpCache
- DumpCookies
- DumpRequestVariable
- DumpResponseVariable
- DumpServerVars
- DumpSession
Utilities.Reflection.ExtensionMethods
- Various reflection related extensions
- CallMethod
- CreateInstance
- DumpProperties
- GetAttribute
- GetAttributes
- GetName
- GetObjects
- GetProperty
- GetPropertyGetter
- GetPropertyName
- GetPropertyType
- GetPropertySetter
- GetTypes
- IsIEnumerable
- IsOfType
- Load
- LoadAssemblies
- MarkedWith
- MakeShallowCopy
- SetProperty
- ToLongVersionString
- ToShortVersionString
Utilities.Environment.ExtensionMethods
- Process related extensions
- KillProcessAsync
- GetInformation
Now we're done... With the Extensions...
Seriously, if you can't find something in here that would make your life a bit easier, you're not doing .Net development.
Where Are My Dragons?
The nice thing about this library is I let you pick what you need. Want to just use the DataTypes and IO namespaces? Not a problem. Want everything? I have a DLL for that also. The easiest approach though is to use NuGet:
Entire library (with command from package manager console):
- Craig's Utility Library: Install-Package CraigsUtilityLibrary
Individual namespaces (with command from package manager console):
- DataTypes: Install-Package CraigsUtilityLibrary-DataTypes
- LDAP: Install-Package CraigsUtilityLibrary-LDAP
- SQL: Install-Package CraigsUtilityLibrary-SQL
- Encryption: Install-Package CraigsUtilityLibrary-Encryption
- Caching: Install-Package CraigsUtilityLibrary-Caching
- Math: Install-Package CraigsUtilityLibrary-Math
- Validation: Install-Package CraigsUtilityLibrary-Validation
- Environment: Install-Package CraigsUtilityLibrary-Environment
- Media: Install-Package CraigsUtilityLibrary-Media
- Web: Install-Package CraigsUtilityLibrary-Web
- ORM: Install-Package CraigsUtilityLibrary-ORM
- Compression: Install-Package CraigsUtilityLibrary-Compression
- Profiler: Install-Package CraigsUtilityLibrary-Profiler
- FileFormats: Install-Package CraigsUtilityLibrary-FileFormats
- Configuration: Install-Package CraigsUtilityLibrary-Configuration
- Random: Install-Package CraigsUtilityLibrary-Random
- DataMapper: Install-Package CraigsUtilityLibrary-DataMapper
- IO: Install-Package CraigsUtilityLibrary-IO
- Reflection: Install-Package CraigsUtilityLibrary-Reflection
- AI: Install-Package CraigsUtilityLibrary-AI
- IoC: Install-Package CraigsUtilityLibrary-IoC
You're Still Here? It's Over. Go Home.
- The library uses the MIT license
- The code is well documented and comes with the needed XML docs through NuGet
- The code has many unit tests that are available with the source.
- I can be reached either on the Github or CodePlex pages.