Chapter 34: a contract, two implementations and two applications, built

If you know Spring or Guice you expect annotations and a container scan. EK9 has neither: a component is a contract, an Application says which implementation stands for it, and a program names the Application it runs under. This lesson builds the notifier example of chapter 34 from the contract up - two implementations, a class that asks for the contract, two applications, two programs - and runs one. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-di-notifiers.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.di.notifiers

  defines class

    OrderService
      notifier as Notifier!

      placeOrder()
        ->
          customer as String
          item as String

        notifier.notify(customer, `Your order for ${item} is confirmed`)

      default operator ?

  defines component

    Notifier as abstract
      notify() as abstract
        ->
          customer as String
          message as String

    EmailNotifier is Notifier
      override notify()
        ->
          customer as String
          message as String

        Stdout().println(`EMAIL to ${customer}: ${message}`)

    ConsoleNotifier is Notifier
      override notify()
        ->
          customer as String
          message as String

        Stdout().println(`CONSOLE [${customer}] ${message}`)

  defines application

    liveApp
      register EmailNotifier() as Notifier

    developmentApp
      register ConsoleNotifier() as Notifier

  defines program

    LiveShop() with application of liveApp
      orders <- OrderService()
      orders.placeOrder("alice@example.com", "a kettle")
      orders.placeOrder("bob@example.com", "two mugs")

    DevelopmentShop() with application of developmentApp
      orders <- OrderService()
      orders.placeOrder("alice@example.com", "a kettle")
      orders.placeOrder("bob@example.com", "two mugs")

Step 1: Notifier — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

Notifier is the component's name. A component is a contract in EK9, not a Spring bean: the abstract one names what any notifier can do and has nothing behind it. Choose Abstract under Extensibility. Choose Preview.

The EK9 IDE just before choosing Preview: Notifier is the component's name

The preview shows Notifier about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Notifier about to be written

Select the Notifier line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the Notifier line and, above it, type the blank line the book opens the block with

Right-click the Notifier line and choose Add Method…

The EK9 IDE once this is done: Right-click the Notifier line and choose Add Method…

notify is the method's name. It is the one thing a Notifier does: it takes a customer and a message and answers with nothing. Add the parameters customer of type String and message of type String and tick abstract - a contract with no body. Choose Apply.

The EK9 IDE just before choosing Apply: notify is the method's name

Step 2: EmailNotifier — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

EmailNotifier is the component's name. It is the production implementation, and Extends is all it takes - there is no annotation and no scan. Type Notifier into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: EmailNotifier is the component's name

The preview shows EmailNotifier about to be written - and, if Notifier owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows EmailNotifier about to be written - and, if Notifier owes any methods, an override stub for each

The override notify() arrived with the component. Select its stub, from require customer? down to its //AI line, and type what an email notifier does. Saves when typed.

The EK9 IDE once this is done: The override notify() arrived with the component

Step 3: ConsoleNotifier — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

ConsoleNotifier is the component's name. It is the second implementation, the one a developer runs without sending real mail. Type Notifier into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: ConsoleNotifier is the component's name

The preview shows ConsoleNotifier about to be written - and, if Notifier owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ConsoleNotifier about to be written - and, if Notifier owes any methods, an override stub for each

Select the stub, from require customer? down to its //AI line, and type what the console notifier does. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require customer? down to its //AI line, and type what the console notifier does

Step 4: OrderService — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

OrderService is the class's name. It is an ordinary class that needs a notifier. It will ask for the CONTRACT, never for EmailNotifier or ConsoleNotifier. Choose Preview.

The EK9 IDE just before choosing Preview: OrderService is the class's name

The preview shows OrderService about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows OrderService about to be written

Select the OrderService line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the OrderService line and, above it, type the blank line the book opens the block with

Right-click the OrderService line and choose Add Field…

The EK9 IDE once this is done: Right-click the OrderService line and choose Add Field…

Type notifier as Notifier!. The ! after the type is the injection point: this property is not set by the constructor, the application it runs under fills it in. Choose OK.

The EK9 IDE just before choosing OK: Type notifier as Notifier!

The preview shows notifier added - and default operator ? with it, OrderService's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows notifier added - and default operator ? with it, OrderService's first field

Right-click the OrderService line and choose Add Method…

The EK9 IDE once this is done: Right-click the OrderService line and choose Add Method…

placeOrder is the method's name. Add the parameters customer of type String and item of type String. Choose Apply.

The EK9 IDE just before choosing Apply: placeOrder is the method's name

Select the stub, from require customer? down to its //AI line, and type the one call placeOrder makes. It calls notify on whatever the application put there. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require customer? down to its //AI line, and type the one call placeOrder makes

Step 5: liveApp — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

liveApp is the application's name. It is production wiring: wherever a Notifier is asked for, the EmailNotifier answers. The Registrations table holds EmailNotifier as Notifier. Choose Preview.

The EK9 IDE just before choosing Preview: liveApp is the application's name

The preview shows liveApp about to be written, with its register line. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows liveApp about to be written, with its register line

Select the liveApp line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the liveApp line and, above it, type the blank line the book opens the block with

Step 6: developmentApp — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

developmentApp is the application's name. It is the same contract wired to the other implementation - a second application is all a development setup takes. The Registrations table holds ConsoleNotifier as Notifier. Choose Preview.

The EK9 IDE just before choosing Preview: developmentApp is the application's name

The preview shows developmentApp about to be written, with its register line. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows developmentApp about to be written, with its register line

Step 7: LiveShop — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

LiveShop is the program's name. It runs with application of liveApp. Type liveApp into Application. Choose Preview.

The EK9 IDE just before choosing Preview: LiveShop is the program's name

The preview shows LiveShop about to be written, with application of liveApp. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows LiveShop about to be written, with application of liveApp

Select the LiveShop line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the LiveShop line and, above it, type the blank line the book opens the block with

Select the stub, from ready <- true down to its //AI line, and type the body: an OrderService and two orders. Nothing here names a notifier. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type the body

Step 8: DevelopmentShop — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

DevelopmentShop is the program's name. It is the same program under the other application. Type developmentApp into Application. Choose Preview.

The EK9 IDE just before choosing Preview: DevelopmentShop is the program's name

The preview shows DevelopmentShop about to be written, with application of developmentApp. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows DevelopmentShop about to be written, with application of developmentApp

Select the stub and type the very same body. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the very same body

Step 9: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Choose DevelopmentShop from the list, then choose Run.

The EK9 IDE just before choosing Run: Choose DevelopmentShop from the list, then choose Run

DevelopmentShop prints the two orders as CONSOLE lines - the same OrderService, a different notifier.

The EK9 IDE once this is done: DevelopmentShop prints the two orders as CONSOLE lines - the same OrderService, a different notifier

Choose Close.

The result

Notifiers.ek9 now reads exactly as chapter 34 of EK9 in Depth shows it. OrderService never names a notifier; the Application a program runs under decides, and swapping EmailNotifier for ConsoleNotifier was a second Application, not a change to OrderService.

The EK9 IDE when the lesson has finished

Where this lesson comes from