Domain events

Whenever a domain object (or list of domain objects) is to be rendered, the framework fires off multiple domain events for every property, collection and action of the domain object. In the cases of the domain object’s collections, the event that is fired is:

  • hide phase: to check that the collection is visible (has not been hidden)

Unlike actions and properties, collections are immutable and so there are no events emitted for disable, validate, pre-execute or post-execute.

Subscribers (which must be domain services) subscribe to events posted through the EventBusService, and can influence each of these phases.

By default the event raised is CollectionDomainEvent.Default. For example:

import lombok.Getter;
import lombok.Setter;

public class ToDoItem {

    @Collection()
    @Getter @Setter
    private SortedSet<ToDoItem> dependencies = ...

    // ...
}

The domainEvent() element allows a custom subclass to be emitted allowing more precise subscriptions (to those subclasses) to be defined instead.

For example:

public class ToDoItem {

    public static class DependenciesCollectionEvent
            extends CollectionDomainEvent<ToDoItem, ToDoItem> { } (1)
    @Collection(
        domainEvent=DependenciesCollectionEvent.class
    )
    @Getter @Setter
    private SortedSet<ToDoItem> dependencies = ...

    // ...
}
1 inherit from CollectionDomainEvent<T,E> where T is the type of the domain object being interacted with, and E is the type of the element in the collection (both ToDoItem in this example)

The benefit is that subscribers can be more targetted as to the events that they subscribe to.

The framework provides a no-arg constructor and will initialize the domain event using (non-API) setters rather than through the constructor. This substantially reduces the boilerplate in the subclasses because no explicit constructor is required..

Subscribers

Subscribers (which must be domain services) subscribe to events posted through the EventBusService.

Subscribers can be either coarse-grained (if they subscribe to the top-level event type):

import org.springframework.stereotype.Service;
import org.springframework.context.event.EventListener;

@Service
public class SomeSubscriber {
    @EventListener(CollectionDomainEvent.class)
    public void on(CollectionDomainEvent ev) {
        // ...
    }
}

or can be fine-grained (by subscribing to specific event subtypes):

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class SomeSubscriber {
    @EventListener(ToDoItem.DependenciesCollectionEvent.class)
    public void on(ToDoItem.DependenciesCollectionEvent ev) {
        // ...
    }
}

The subscriber’s method is called (up to) 5 times:

  • whether to veto visibility (hide)

  • whether to veto usability (disable)

  • whether to veto execution (validate) the element being added to/removed from the collection

  • steps to perform prior to the collection being added to/removed from

  • steps to perform after the collection has been added to/removed from

The subscriber can distinguish these by calling ev.getEventPhase(). Thus the general form is:

 import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class SomeSubscriber {

    @EventListener(CollectionDomainEvent.class)
    public void on(CollectionDomainEvent ev) {
        switch(ev.getEventPhase()) {

            case HIDE:                      (1)
                break;
            case DISABLE:                   (2)
            case VALIDATE:
            case EXECUTING:
            case EXECUTED:
                break;
        }
    }
}
1 call ev.hide() or ev.veto("") to hide the collection
2 collections are immutable and so domain events are never called for these phases.

Default, Doop and Noop events

If the domainEvent() element is not explicitly specified (is left as its default value, `CollectionDomainEvent.Default), then the framework will, by default, post an event.

If this is not required, then the causeway.reflector.facet.collectionAnnotation.domainEvent.postForDefault configuration collection can be set to "false"; this will disable posting.

On the other hand, if the domainEvent has been explicitly specified to some subclass, then an event will be posted. The framework provides CollectionDomainEvent.Doop as such a subclass, so setting the domainEvent attribute to this class will ensure that the event to be posted, irrespective of the configuration collection setting.

And, conversely, the framework also provides CollectionDomainEvent.Noop; if domainEvent attribute is set to this class, then no event will be posted.

Raising events programmatically

Normally events are only raised for interactions through the UI. However, events can be raised programmatically either by calling the EventBusService API directly, or by emulating the UI by wrapping the target object using the WrapperFactory domain service.