concept implementation in category dsl

appears as: implementation, implementation, implementations, n implementation, The implementation, implementations
DSLs in Action

This is an excerpt from Manning's book DSLs in Action.

This DSL script has an underlying semantic model; the implementation is in the form of Java classes, methods, and packages that create interfaces for tasks and dependencies. The developer doesn’t have to cross the boundaries of the DSL interface and dig down into the implementation in order to use Ant. Of course, there might be an exceptional situation when the developer might need to do so, because Ant is an extensible framework. But that’s only the exception.

Listing 4.2. Expressive domain vocabulary in implementation of Account
class Account attr_reader :no, :names, :addr, :type, :email_address def number(number) @no = number end def holders(*names) @names = names end def address(addr) @addr = addr end def type(t) @type = t end def email(e) @email_address = e end def to_s() "No: " + @no.to_s + " / Names: (" + @names.join(',').to_s + ") / Address: " + @addr.to_s end end

The domain vocabulary is explicit in the implementation and we don’t have much noise from accidental complexities polluting the essence of the domain model. (For more about accidental complexity, see appendix A.) Because this is the first of the domain models that we’ll carve out in this chapter, let’s look at some of the Scala features that make this model expressive yet concise.

Table 6.7. Composing DSLs hierarchically

Associated abstractions need to evolve independently

Balance is: Client portfolio is:
  • An amount of money or security held by a client
  • An important domain concept with specific semantics that can be modeled as a DSL
  • An amount that can be expressed as BigDecimal for implementation purposes only, though BigDecimal doesn’t have any meaning to the domain user
  • A report of a client’s balances across his holdings
  • An important domain concept with specific semantics that can be modeled as a DSL
Note: You should always hide your implementation from your published language constructs. Not only does this method make your DSL readable, it lets you change the underlying implementations seamlessly without any impact on the client code. To learn more about how to hide your implementation, read the discussions that are in appendix A. Modeling the association: A snippet from the point of view of the domain user clearly shows how the two abstractions for balance and portfolio can be associated when you’re designing domain APIs: trait Portfolio {
def currentPortfolio(account: Account): Balance
} To Do: You need to compose the two DSLs such that the association can be seamless across multiple implementations of each of them. You can flexibly plug in an alternative implementation of Balance DSL when defining an implementation of the Portfolio DSL.

Balance is the interface that abstracts the underlying implementation. Scala lets you define type synonyms. You can define type Balance = BigDecimal and happily use Balance as the name to describe the net value of your client holdings in his portfolio. But what happens when you build larger families of Portfolio DSLs as specializations of your base DSL, just like the ones we made with TradeDsl in section 6.7.1? Embedding the implementation of Balance within the Portfolio DSL will couple the entire hierarchy to a concrete implementation. Even if you need to, you’ll never be able to change the implementation in the future. The solution is to avoid directly embedding one DSL in the other and instead compose them hierarchically. In the end, you’ll have two DSLs that fit together nicely and that are coupled loosely enough so that you can plug in your implementation of choice whenever you feel like it.

Listing 6.19. A DSL with implementation coupling

Ugh! By the time you get down to the first specialization of the Portfolio DSL, you see that the Balance abstraction has already been broken through . Try to compose them hierarchically and keep the implementation of the Balance DSL outside the Portfolio DSL. Composing hierarchically means that one needs to be within the other. The difference between composing hierarchically and the code in listing 6.19 is that you are going to embed the DSL interface and NOT the implementation. I can almost hear the murmur of abstract vals coming across from my readers. You guessed it! The Portfolio DSL needs to have an instance of the Balance DSL, which we’ll call Balances.