Search code examples
theorydsl

What is a DSL and where should I use it?


I'm hearing more and more about domain specific languages being thrown about and how they change the way you treat business logic, and I've seen Ayende's blog posts and things, but I've never really gotten exactly why I would take my business logic away from the methods and situations I'm using in my provider.

If you've got some background using these things, any chance you could put it in real laymans terms:

  • What exactly building DSLs means?
  • What languages are you using?
  • Where using a DSL makes sense?
  • What is the benefit of using DSLs?

Solution

  • DSL's are good in situations where you need to give some aspect of the system's control over to someone else. I've used them in Rules Engines, where you create a simple language that is easier for less-technical folks to use to express themselves- particularly in workflows.

    In other words, instead of making them learn java:

    DocumentDAO myDocumentDAO = ServiceLocator.getDocumentDAO();
    for (int id : documentIDS) {
    Document myDoc = MyDocumentDAO.loadDoc(id);
    if (myDoc.getDocumentStatus().equals(DocumentStatus.UNREAD)) {
        ReminderService.sendUnreadReminder(myDoc)
    }
    

    I can write a DSL that lets me say:

    for (document : documents) {
    if (document is unread) {
     document.sendReminder
    }
    

    There are other situations, but basically, anywhere you might want to use a macro language, script a workflow, or allow after-market customization- these are all candidates for DSL's.