I’m a huge fan of DSL these days and try to implement wherever possible. I first heard of DSL through Hibernate query DSL, then groovy lang, Proto Buffers and so on.
Martin Fowler would define DSL as A a computer lang that's targeted to a particular kind of problem, rather than a general purpose lang that's aimed at any kind of software problem.
I found some libraries like hibernate dsl making their own embedded DSL, which we have to follow as it is. But there’s always a royal road for us to create our own DSL to solve our domain problems.
So, Internal DSLs aka embedded DSL aka Fluent Interface ( FI ) aka semantic facades are particular ways of using a host language (say Java) to give the host language the feel of a particular language.
eg. in Elasticsearch Java API,
AggregationBuilder aggregationBuilder =
(DateRangeBuilder)AggregationBuilders.dateRange("dateRangeAggs")
.field("transactionDate")
.addRange("Oct-1989", "1989-10-28", "1990-10-28")
.addRange("Nov-1990", "1990-10-28", "1991-10-28")
.subAggregation(AggregationBuilders.sum("transactionAmtSum")
.field("transactionAmt"))
.subAggregation(AggregationBuilders.avg("balanceAvg")
.field("balanceAmt"));
Internal DSLs are about readability.
Other internal DSL examples
Squill
While External DSLs have their own custom syntax and we write a full parser to process them.
eg. 1)
/**
*/
servers = {
clusterName("gccount cluster")
server(){
name("Node1")
hostname("localhost")
port("9160")
keyspace("gccount")
}
But we need a parser for above DSL, in groovy there’s groovy.util.BuilderSupport for creating arbitrary nested trees of objects. In Java we can write parsers as here.
References
No comments:
Post a Comment