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:
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:
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();
And here's the same using a fluent interface:
I would like to know what you think of this approach.Please let me know