Hello Fabrik.Common and fun with strings

Like many developers, over time I've gathered code that makes writing applications a little easier - whether it be commonly used utility functions, extensions on existing .NET types or full blown abstraction layers for working with databases/file systems.

Much of this code was written whilst developing fabrik, probably my most challenging project to date. After working on a few other projects recently I realized how much I relied on this code and therefore decided to compile it into a reusable library.

Furthermore, I wanted to release it as open source, after all, a lot of it came from or was inspired by open source.

Rather than just dumping masses of code on GitHub without any explanation I'm actively working on thoroughly documenting the project. It's also serving as a great way for me to learn TDD/BDD so be sure to check out the provided tests.

So far I've only added the core Fabrik.Common library which mainly contains utility and extension methods. However, I'm working through many of the applications I've created recently to find other useful stuff to share. In particular, look out for Fabrik.Common.Web in the coming weeks that is chock full of MVC code that I just can't live without.

Fabrik.Common is now available on GitHub for you to use, copy, change, fork, contribute or criticize.

As a bit of an introduction I thought I'd document some of the string extensions available in Fabrik.Common:

IsNullOrEmpty

Instead of writing:

if (string.IsNullOrEmpty(someString))

You can just write:

if (someString.IsNullOrEmpty())  

IsNotNullOrEmpty

Similarly, I don't like writing:

if (!string.IsNullOrEmpty(someString))

So we have a better extension:

if (someString.IsNotNullOrEmpty())

The intent of the code is much clearer.

FormatWith

String.Format just plain sucks. This is much nicer:

"Hey there {0}, do you like {1}?".FormatWith("John", "Pizza");

NullIfEmpty

This allows you to safely use the null coalescing operator with strings:

var name = someString.NullIfEmpty() ?? "Default name";

ToSlug

A tried and tested "slugifier" function:

result = " Boom!#% // What's \\ (up) 'with' the £$price # of **fuel** these days?!-".ToSlug();

// boom-whats-up-with-the-price-of-fuel-these-days

ToSlugWithSegments

In case you want to allow segments in your slug e.g. archive/posts/foo (we use this regularly in CMS type projects):

result = @"/blogs/archived posts/2012/".ToSlugWithSegments();

// blogs/archived-posts/2012

We've also provided some useful string validation extensions:

IsValidSlug

Tests if a string is a valid slug:

"Is this valid".IsValidSlug(); // false

IsValidUrl

Tests if a string is a valid URL:

"http://ben.onfabrik.naughty".IsValidUrl(); // false

IsValidEmail

Tests if a string is a valid email address:

"yourname@somedomain.com".IsValidEmail(); // true

IsValidIPAddress

Tests if a string is valid IP Address:

"256.60.124.136".IsValidIPAddress(); // false

You'll find the underlying regular expressions for the above methods within the Fabrik.Common.RegexUtils class.

Look out for more Fabrik.Common goodness in the coming weeks.


Ben Foster

About Me

I'm a software engineer and aspiring entrepreneur with 12+ years experience in the tech industry and have worked with startups and SMB’s in areas such as healthcare, recruitment and e-commerce (I even worked in enterprise, once). I founded my first startup Fabrik in 2011.

I now head up the engineering team at Checkout.com. If you're interested in working in an exciting fin-tech company, drop me a message on Twitter.

Creative Commons Licence