July 5, 2012

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:


Instead of writing:

if (string.IsNullOrEmpty(someString))

You can just write:

if (someString.IsNullOrEmpty())  


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.


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

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


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

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


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


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:


Tests if a string is a valid slug:

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


Tests if a string is a valid URL:

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


Tests if a string is a valid email address:

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


Tests if a string is valid IP Address:

"".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.

© 2022 Ben Foster