Bookmark and Share

Fluent.NET 1.0.0.0 Released

by KodefuGuru 3. January 2010 17:04

The first release of Fluent.NET is in the wild. It’s not a lot right now, but it is useful, and it will grow as I come across .NET framework methods that should be more fluent. So perhaps I should explain what Fluent.NET is.

As an English speaker (a fair assumption since you’re reading my blog), you probably read from left to right. Why then do we as C# developers read and write code from the outside in?

File.WriteAllLines("new.txt", File.ReadAllLines("old.txt"));

Fluent.NET adds extension methods to give the functionality where it truly belongs. This fits in with a design princple, Strive for Functional Cohesion. In the above example, the WriteAllLines method logically belongs to File, but functionally it belongs to is IEnumerable<String>. In the Fluent.NET framework, IEnumerable<String> was extended so it now has the method Write.

File.ReadAllLines("old.txt").Write("new.txt");

This has the added benefit of allowing us to read and write code like we do natural (albeit Western) languages, from left to right.

Fluency involves method chaining, and in this case it is no different. ReadAllLines returned an array of strings, which we can then call a Linq Select statement on to transform it into something else. Assume that the text file being read contains comma-separated values. The text file being written to should have those values in reverse. This can easily be written as the following snippet of code.

File.ReadAllLines("old.txt")
    .Select(s => s.Split(',').Reverse().Delimit(","))
    .Write("new.txt");

Delimit is another function introduced in Fluent.NET. It is a pass-through method to String.Join.

Here’s a small program that introduces two things within Fluent.NET.

1.To(5).Execute(Console.WriteLine);

The To extension method simply creates an IEnumerable<int> from 1 int to another (up or down). You can create an array, but who wants to write all those elements out? You could also have used Enumerable.Range, but I felt that was clunky and not exactly fluent as you can’t start from any integer (it’s probably more approprite to do start.To(end) in a real program).

Execute is what most people have created as ForEach in their own library of extension methods. I have a problem with the name ForEach. ForEach implies the keyword in C#, and many of the Linq extension methods do a foreach on the sequence anyway. The name means nothing. Execute was chosen because an action is being immediately executed on each element in the sequence, in this case Console.WriteLine.

Fluent.NET does contain an Each method as well. This method is similar to the Execute method except that it is deferred and it returns the element it acts upon.

Fluent.NET currently contains two assemblies: Fluent.dll and Fluent.Web.dll. One goal is to not tie users of Fluent to assemblies they don’t require, so each assembly will be related to the .NET assembly it is making fluent. This led to an odd design decision: I did not wish for duplicate namespaces everywhere. To solve this, I made every Fluent class belong to the Fluent namespace. To turn Fluent on in a file, add the appropriate assemblies, use the Fluent namespace, and you’re good to go.

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
C# MVP, INETA Community Champion


MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er

I have joined Anti-IF Campaign


World Map

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010