Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Palying with fluent interfaces

, 14 Sep 2009
Rate this:
Please Sign up or sign in to vote.
A while ago I was reviewing one of our projects code and I noticed that it was very complicated and I had a hard time understanding what was going on and I was one of the senior developers of the team.I thought to myself maybe we could have a more readable approach in coding so that it would be easi

A while ago I was reviewing one of our projects code and I noticed that it was very complicated and I had a hard time understanding what was going on and I was one of the senior developers of the team.I thought to myself maybe we could have a more readable approach in coding so that it would be easier to understand what is the intention of the code.

I think that using DSLs can help us to improve readability of our codes.As Martin Fowler teaches us in his brilliant book about DSL there are mainly two kind of DSLs internal and external and there's this concept of "fluent interface" that let us implement a kind of internal DSL.

Since I'm working with C# 3.0 and as far as I know C# is not a DSL friendly language I think using this fluent interface concept can be a good candidate to improve our code and have some useful DSLs in place.But how?

Suppose that we have a Login method in our domain that lets us to authenticate a user using a login name and a password,Something like this:

UsersDomain.Login(username,password);

Actually every method like this is an action/command executing on a specific domain.Then we can have our domain specific language as follows:

On Domain Do Action With Patameters

And here's a C# example of the same:

userDomain.Login().With((action)=>{action.Username=username;
action.Password=password;}).Do();

I know this seems very verbose in comparison with our traditional version but if we have thousands lines of code combining different domains and logic declaring everything in the latter way should help us understand the code better.

Let me show you a better example:

Suppose we have a mailing application that lets a client to send an email and here's our traditional approach to implement it:

var mail=new Mail();
mail.From=fromAddress;
mail.To=toAddress;
mail.Header=header;
mail.Body=body;
mail.Send();

And here's the same using a fluent interface:

mail.Send().From(fromAddress).To(toAddress).Header(header).Body(body).Do();

I would like to know what you think of this approach.Please let me know

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

beatles1692
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembercs_dilo24-Sep-09 3:59 
GeneralMy vote of 1 Pinmembercsb121-Sep-09 4:48 
GeneralRe: My vote of 1 Pinmemberbeatles169221-Sep-09 10:46 
QuestionWhy Not... PinmemberInfRes7-Sep-09 22:57 
AnswerRe: Why Not... Pinmemberbeatles169213-Sep-09 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 14 Sep 2009
Article Copyright 2009 by beatles1692
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid