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.

Tags: , , ,

Kodefu

Comments

1/3/2010 5:55:08 PM #

anonymous

Your '.NET Community Champion' title is hilarious.

anonymous United States

1/3/2010 7:55:48 PM #

Chris

What's so hilarious about it?

www.inetachamps.com/Profile/Details/ChrisEargle

Chris United States

1/3/2010 10:42:14 PM #

Justin James

My suspicion is that the "inside out" reading made a *lot* more sense to the average programmer when "computer science" was a subset of math, not "how to glue together a Web site in Java", and even before that, when more "programmers" were really math/science people. Along the same lines, the RPN that Lisp uses makes a TON of sense... if you are a math/science person. Smile

J.Ja

Justin James United States

1/4/2010 12:56:07 AM #

Justin James

Wow, you played L.O.R.D. too? I was a master at sweet talking Violet, the barmaid. Smile

J.Ja

Justin James United States

1/4/2010 2:29:36 AM #

Chris

Heh, I not only played LORD, I wrote mods for it =). I used to mess around with the guys who sweet talked Violet on my BBS with some custom events.

Chris United States

1/4/2010 1:19:27 PM #

John Sonmez

I remember LORD... LORD and Tradewars.  

Nice job on the fluent library.  It makes a lot of sense to me.  The only problem is that if you use it there are more ways to do the same thing, which could make things more complicated.  But I really like the idea.  It is very much the LINQ/Lamba style thinking transformation, which I am convinced in the next step for high level languages.  Just like collections/containers transformed how we solved problem and thought about problems.

Keep it up, I am interested to see how this evolves.

John Sonmez United States

1/4/2010 1:59:20 PM #

chris

I played Tradewars as well! Did you play Barren Realms Elite?

I'm trying to only add things to the library if it adds value. If it prevents you from going to the start of a line to add something, it's more fluent and it adds value (hence IsNullOrEmpty()). I prefer left to right versus outside to inner. This is the reason I didn't add IsNull to Object.

Consider:
obj.IsNull()
vs
obj == null

I don't think there's anything to be gained from having an IsNull method, so I figured it would be a junk method and didn't add it.

chris United States

1/4/2010 2:25:30 PM #

Justin James

I've gotta say Chris, this is definitely the best example I've seen from you as to why Fluent is useful/usable, particularly the bit about "left to right" compared to "inside out". Put like that, I see how it resolves a major complaint I've had about C-style OO languages for a long time. This feels a lot more useful to me than many of the other "refactor for readability" things I've seen written. I couldn't care less about how things align on the page, but this is actually, truly useful and slashes SLOC in a way that doesn't get "too clever".

J.Ja

Justin James United States

1/6/2010 4:51:13 PM #

JP

Hi, Chris

Thanks for this library. I was browsing the code, and have a problem to build, namemy because of the Contract. The projects and solution seem to be Visual Studio 2008, but they contain a reference to System.Diagnostics.Contracts, which is AFAIK a Visual Studio 2010 library. Have you copied it to 2008 ? Do we have to reference additional libs ?

Thanks in advance

JP France

1/25/2010 6:54:29 AM #

Best Hardware Guides

it seems to me that this is more or less like MonoRocks....i have used the latter and they both seem the same is it true??

Best Hardware Guides United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

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

I am a #52er


World Map

RecentComments

Comment RSS

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