Lunch and Learn playlist 2017

Listing here the 46 talks watched by my team at Dell during the 2017 Lunch and Learn sessions.

To know more about facilitating tech lunches take a look at my post One year of Lunch and Learn

  • 11/01/2017 – The Error of Our Ways, Kevlin Henney (50m)
  • 18/01/2017 – Want to be a Better Programmer?, Lars Bak & Kasper Lund (1h 5m)
  • 25/01/2017 – Agile is Dead, Dave Thomas (40m)
  • 01/02/2017 – Practical Techniques for Repaying Technical Debt, Mark Heath
  • https://app.pluralsight.com/library/courses/understanding-eliminating-technical-debt (module 6, 25min)

  • 08/02/2017 – DDD & REST – Domain Driven APIs for the web. Oliver Gierke (1h 15m)
  • 15/02/2017 – How I Finally Stopped Worrying and Learnt to Love Conway’s Law, James Lewis (40m)
  • 22/02/2017 – Derek OKeeffe on Big Data Lake
  • 01/03/2017 – The Open Closed Principle, Steve Smith
  • https://app.pluralsight.com/library/courses/principles-oo-design (module 2, 28min)

  • 08/03/2017 – The art of destroying software, Greg Young (42m)
  • 15/03/2017 – The frontend taboo: a story of full stack micro services, Moritz Grauel (48m)
  • 22/03/2017 – Building Teams Who Build Data Products, Peter Holford (28m)
  • 29/03/2017 – Continuous Architecture, Murat Erder (32m)
  • 05/04/2017 – Property-Based Testing for everyone by Romeu Moura (49m)
  • 12/04/2017 – When Pairing goes Pear-Shaped, Pivotal (47m)
  • 19/04/2017 – What the… JavaScript?, Kyle Simpsons (38m)
  • 26/04/2017 – Lecture on creativity – John Cleese
  • 03/05/2017 – Stop Treading Water: Learning to Learn, Edward Kmett (40m)
  • https://yow.eventer.com/yow-2014-1222/stop-treading-water-learning-to-learn-by-edward-kmett-1750

  • 10/05/2017 – Systems programming as a swiss army knife, Julia Evans (36m)
  • 24/05/2017 – Principles Of Microservices, Sam Newman (1h)
  • 31/05/2017 – Feature Branches And Toggles In A Post-GitHub World, Sam Newman (49m)
  • https://gotochgo.com/2017/sessions/45

  • 07/06/2017 – If Agile is so good, why are our Products so bad?, Gabrielle Benefield (46m)
  • 21/06/2017 – Managing Manager‐less Processes, Fred George (52m)
  • 28/06/2017 – Programming Across Paradigms, Anjana Vakil (49m)
  • 05/07/2017 – The Many Meanings of Event-Driven Architecture, Martin Fowler (50m)
  • 12/07/2017 – Building Modern APIs with GraphQL, Robert Zhu (49m)
  • 19/07/2017 – Front Ends for Back End Developers, Matt Raible (49m)
  • 26/07/2017 – Seven Ineffective Coding Habits of Many Programmers, Kevlin Henney (46m)
  • 02/08/2017 – Reactive Microservices with DDD and Actors, Vaughn Vernon (48m)
  • 09/08/2017 – Microservices and Kubernetes: New functionality to build and operate apps. (45m)
  • 16/08/2017 – Lean UX for Startups and Enterprise: Ten Secrets to Success (33m)
  • 23/08/2017 – Kubernetes for Pythonistas, Kelsey Hightower (33m)
  • 30/08/2017 – Better Functional Architecture with Redux, Yuri Takhteyev (36m)
  • 06/09/2017 – Lightning talks from RebelCon Conference 2017
  • https://www.youtube.com/channel/UCQGeTJ1HbnZQN5NU_Tf7otg

  • 13/09/2017 – Java 9: Make Way for Modules!, Mark Reinhold (29m)
  • 20/09/2017 – The Tech Industry needs all Kinds of Minds – How to Support Them?, Sallyann Freudenberg (43m)
  • 27/09/2017 – What can Kotlin do for me?, Hadi Hairi (30m)
  • 04/10/2017 – One Size Does Not Fit All, Stefan Tilkov (45m)
  • 11/10/2017 – Kill “Microservices” before its too late, Chad Fowler (39m)
  • 25/10/2017 – Software-Defined Culture, Tim Gross (47m)
  • 01/11/2017 – There is No Spotify Model, Marcin Floryan (44m)
  • https://www.infoq.com/presentations/spotify-culture-stc

  • 08/11/2017 – Keynote: 8 Lines of Code, Greg Young (45m)
  • https://www.infoq.com/presentations/8-lines-code-refactoring

  • 15/11/2017 – The Future of Programming, Bret Victor (33m)
  • 22/11/2017 – Forget Velocity, Let’s Talk Acceleration. Jessica Kerr (54m)
  • 29/11/2017 – How Spotify works, Ruairi O’Brien
  • 06/12/2017 – Compositional UIs – the Microservices Last Mile, Jimmy Bogard (55m)
  • 20/12/2017 – Team autonomy vs agreed best practice – A sizable conundrum. Laurence Dusoswa (24m)
Advertisements

DDD – Aggregate Roots and Domain Events publication

Implementing DDD in a traditional object-oriented style *, when an action over an Aggregate succeeds the state of the aggregate changes and one or multiple domain events are published.
A Domain Event represents something that has happened in the domain. In the other hand if the action cannot be fulfilled a Domain Error is thrown as an exception by the aggregate.

Publishing Domain Events from an Aggregate
The question that arises here is Should the aggregates publish events directly?. We can find a multitude of articles and examples where the Aggregate implementation does exactly that. The very same Vaughn Vernon’s IDDD book shows this approach (page 300):

public class BacklogItem extends ConcurrencySafeEntity {
  ...
  public void commitTo(Sprint aSprint) {
  ...
    DomainEventPublisher
      .instance()
      .publish(new BacklogItemCommitted(
         this.tenantId(),
         this.backlogItemId(),
         this.sprintId()));
    }
  ...
}

the BacklogItem Aggregate creates and dispatches an event directly using a DomainEventPublisher instance before the database transaction is commited.

This approach can become quite complex to implement right, because of the transactional concerns. Think about it, if the aggregate publishes events directly, what happens if later on the persistence operation fails?
Unit testing of aggregates gets also complicated, the aggregate is coupled to a DomainEventPublisher that it instantiates directly. At the same time, we know that we should avoid dependency injection into an Aggregate.

An alternative approach is to use a Two-steps event publication

  • 1st) Aggregate root creates and registers domain events for publication.
  • 2nd) When the application Service persists the changes to the aggregate (calling the save method in a Repository) the infrastructure takes care of publishing the registered events only if the save transaction succeeds.

Let’s see this in action with some code. The domain example is for a Runbook which tracks Tasks, and the use case is assigning a task to an operator.

– Service retrieves and acts on the aggregate.

// RunbookService
fun assignTask(c: AssignTask) {
    val runbook = runbookRepository.findById(c.runbookId)

    runbook.assignTask(c.taskId, c.assigneeId, c.userId) // act on the aggregate
    ...

– Aggregate changes its state and then creates and registers domain events.

// Runbook
fun assignTask(taskId: String, assigneeId: String, userId: String) {
    validateIsOwner(userId)

    getTask(taskId).assign(assigneeId)

    registerEvent(TaskAssigned(runbookId, taskId, assigneeId)) // create and register event
}

Notice that Domain Events are only registered by the aggregate root because it is the one ensuring the integrity of the aggregate as a whole.

The way that the aggregate “registers” events is keeping track of them internally as a list of unpublished domain events.

...
@Transient
private val domainEvents = ArrayList()

protected fun registerEvent(event: EventType) {
    this.domainEvents.add(event)
}
...

– Service calls repository to save changes
After calling the assignTask action on the aggregate the service saves the changes (the “changed” aggregate) using the repository.

// RunbookService
fun assignTask(c: AssignTask) {
    val runbook = runbookRepository.findById(c.runbookId)

    runbook.assignTask(c.taskId, c.assigneeId, c.userId)

    runbookRepository.save(runbook) // save aggregate
}

– Infrastructure publishes all the aggregate unpublished domain events.
This can be done directly with a custom repository implementation and some form of internal pub-subscribe mechanism or with the help of a framework. Later on, I describe how to do this for repositories based on Spring Data.

Unit Testing
Unit testing of the aggregate remains simple with this design, there is no need to mock an event publisher or anything else. Asserting that the aggregate created and registered the right events can be done directly checking the aggregate domain events collection.

    @Test
    fun `can assign task`() {
        val runbook = Runbook(RUNBOOK_ID, RUNBOOK_NAME, OWNER_ID)
        runbook.addTask(TASK_ID, TASK_NAME, TASK_DESCRIPTION)

        runbook.assignTask(TASK_ID, TASK_ASSIGNEE_ID, OWNER_ID)

        assertThat(runbook.domainEvents()).contains(TaskAssigned(RUNBOOK_ID, TASK_ID, TASK_ASSIGNEE_ID))
    }

Domain Events
Here is an example of how the Domain Events for an aggregate look using Kotlin sealed classes.

sealed class RunbookEvent

data class TaskAdded(
        val runbookId: String,
        val taskId: String) : RunbookEvent()

data class TaskAssigned(
        val runbookId: String,
        val taskId: String,
        val assigneeId: String) : RunbookEvent()
...

Using Spring Data infrastructure to Publishing Events from Aggregate Roots
Spring Data repositories have support for aggregate domain events publication. On a call to a repository save() method, Spring will look into the passed aggregate for any unpublish domain events and will automatically publish them via its ApplicationEventPublisher API.

To expose the events the aggregate root needs to provide an accessor method annotated with the @DomainEvents annotation.

...
@Transient
private val domainEvents = ArrayList()
 
@DomainEvents
fun domainEvents(): Collection<EventType> {
    return Collections.unmodifiableList<EventType>(domainEvents)
}
...

Aggregate Root base class
We can reuse across all aggregates the same code for registering events placing it in an AggregateRoot base class.

abstract class AggregateRoot<EventType> {

    @Transient
    private val domainEvents = ArrayList<EventType>()

    /**
     * All domain events currently captured by the aggregate.
     */
    @DomainEvents
    fun domainEvents(): Collection<EventType> {
        return Collections.unmodifiableList<EventType>(domainEvents)
    }

    /**
     * Registers the given event for publication.
     */
    protected fun registerEvent(event: EventType): EventType {
        this.domainEvents.add(event)
        return event
    }

    /**
     * Clears all domain events currently held. Invoked by the infrastructure in place in Spring Data
     * repositories.
     */
    @AfterDomainEventPublication
    protected fun clearDomainEvents() {
        this.domainEvents.clear()
    }
}

Conclusion
Domain Events make explicit what changes within a domain. Use them not only to have a richer model but also a better design, implementation and tests. The different parts of the system can be decoupled much easily and prevent us from having large application services or aggregates.
It also simplifies future integration with other systems, translation from Domain Events to messaging Integration Events is trivial.

References
– Implementing Domain Driven Design, Vaughn Vernon.
– A better domain events pattern, Jimmy Bogard. https://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern
– Spring – Domain event publication from aggregate roots, Mark Paluch. https://spring.io/blog/2017/01/30/what-s-new-in-spring-data-release-ingalls#domain-event-publication-from-aggregate-roots
– Domain Events vs. Integration Events in Domain-Driven Design and microservices architectures, Cesar de la Torre
https://blogs.msdn.microsoft.com/cesardelatorre/2017/02/07/domain-events-vs-integration-events-in-domain-driven-design-and-microservices-architectures/
– Domain events: design and implementation. https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/domain-events-design-implementation

1 – In a functional DDD style the aggregates: do not mutate state; do not publish or register events; do not throw exceptions. Instead, they return a result type containing the new version of the aggregate and domain events or in case of failure, the result contains the domain errors.

Angular pluggable architecture?

Pluggable Architecture – A Software Architecture that allows to plug functionality using PluggableModules. Ward Cunningham

One of the projects I am involved in these days is composed of multiple applications each of them with its own front-end implemented in Angular, all using consistent UX patterns and framework. The common user flows like managing projects or navigating to the applications is handled in a central application. At the same time, common components like navbar or sidebars are reused across.

This front-end separation by applications or large features has prevented us from creating a big monolithic web app and our teams have the kind of independence and control in the front-end we get in the back-end with micro-services.

All good so far, but let’s say that now we want to have a dashboard application composed of widgets and one of the requirements is that multiple teams can implement and deploy their own widgets independently. Hence, without the need of modifying the dashboard application code or redeploying it when a new widget comes or gets updated.

Can an Angular CLI application load and render external widgets at runtime?

The short answer is yes, it is possible. You can find a working demo in this repo https://github.com/paucls/angular-pluggable-architecture

The long answer is that there is not a straightforward solution ;-), it took me a little bit of research to find out how.

First of all, the application needs a way to load remote widget bundles at runtime. This sounds like lazy loading, but that is not going to help us, lazy loading is for loading modules linked to child routes, but the modules still need to belong to the same application and be known at build-time.

The solution is to use SystemJS, but only at runtime and only to load the widgets. We do not need to configure our CLI project to switch from Webpack to SystemJS.

Loading a bundle will look like this:

const module = await SystemJS.import('/path-to-a-module-bundle');

The next problem is that the bundle contains a Module and a Component for the widget but they have dependencies themselves to other modules. The smallest component has at least dependencies to @angular/core.

import { Component, OnInit } from '@angular/core';

To stop SystemJS from trying to find and fetch those vendor modules from the network we need to set SystemJS with the modules already available.

/**
 * Set existing vendor modules into SystemJS registry.
 * This way SystemJS won't make HTTP requests to fetch imported modules
 * needed by the dynamicaly loaded Widgets.
 */
import { System } from 'systemjs';
declare const SystemJS: System;

import * as angularCore from '@angular/core';
import * as angularCommon from '@angular/common';
import * as angularCommonHttp from '@angular/common/http';

SystemJS.set('@angular/core', SystemJS.newModule(angularCore));
SystemJS.set('@angular/common', SystemJS.newModule(angularCommon));
SystemJS.set('@angular/common/http', SystemJS.newModule(angularCommonHttp));

The second part is compiling and rendering each widget. For this, we need to make use of the Angular JIT compiler to compile the Angular module and component of the widget. And then create the widget component in ViewChield target div.

@Component({
  selector: 'app-dashboard',
  template: '<div #content></div>'
})
export class DashboardComponent implements AfterViewInit {

  @ViewChild('content', { read: ViewContainerRef }) content: ViewContainerRef;

  constructor(private compiler: Compiler, private dashboardService: DashboardService,
    private injector: Injector) { }

  ngAfterViewInit() {
    this.loadWidgets();
  }

  private async loadWidgets() {
    const widgets = await this.dashboardService.getWidgetConfigs().toPromise();
    widgets.forEach((widget) => this.createWidget(widget));
  }

  private async createWidget(widget: WidgetConfig) {
    // import external module bundle
    const module = await SystemJS.import(widget.moduleBundlePath);

    // compile module
    const moduleFactory = await this.compiler.compileModuleAsync(module[widget.moduleName]);

    // resolve component factory
    const moduleRef = moduleFactory.create(this.injector);
    const componentProvider = moduleRef.injector.get(widget.name);
    const componentFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(componentProvider);

    // compile component
    this.content.createComponent(componentFactory);
  }

}

Note that to be able to use the JIT compiler our build needs to have AOT disabled what it is not ideal. There is an open issue (https://github.com/angular/angular/issues/20875) to allow the use of the compiler with AOT, I tried some of the temporal solutions suggested like defining custom providers for JIT in the app module but I could not make it work yet.

References:
– Developing with Angular, Denys Vuika. https://github.com/DenisVuyka/developing-with-angular/tree/master/angular/plugins
– As busy as a bee — lazy loading in the Angular CLI, David Herges https://blog.angularindepth.com/as-busy-as-a-bee-lazy-loading-in-the-angular-cli-d2812141637f … how does lazy loading in the Angular CLI work under the hood?
– How to load dynamic external components into Angular application? https://stackoverflow.com/questions/45503497/how-to-load-dynamic-external-components-into-angular-application/45506470#45506470
– Here is what you need to know about dynamic components in Angular, Maxim Koretskyi https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e
– Modules are not what you think they are, Maxim Koretskyi https://youtu.be/pERhnBBae2k
– Extension mechanism demo https://github.com/maximusk/extension-mechanism-demo
– JIT Compiler needed with AOT Build for Dynamic Component. https://github.com/angular/angular/issues/20875