Folks,
Looking for opinions here.
I recently committed an EventBusService, so that one can programmatically
post an event via Guava's EventBus:
public void setStartDate(LocalDate dt) {
this.setStartDate = dt;
eventBusService.post(new StartDateChangedEvent(this, oldValue,
newValue), getFoos(), getBars(), ...)
}
Guava then invokes any object that has a method annotated @Subscribe
accepting the event type:
@Subscribe
public void handle(StartDateChangedEvent ev) { ... }
It's trivially easy for domain objects to register themselves with the
event bus. In their injectXxx method, just do the register:
public void injectEventBusService(EventBusService ebs) {
this.eventBusService = ebs;
ebs.register(this);
}
I'm reasonably happy with all of this, I think, though the downside is that
the publisher needs to ensure that any potential subscribers are in-memory
so that they get notified. This is the purpose of the getFoos() and
getBars() calls passed in to post(...); it's a hint for Isis to bring the
objects in these collections into memory.
Also, strictly speaking, the publication of the event should be in
modifyStartDate(...) rather than setStartDate(...) to avoid unintended
side-effects with JDO when it calls setStartDate to recreate or delete the
object. In fact, the JDO objectstore does protect against this, but it's
all somewhat hacky.
~~~
I'm now thinking about introducing an annotation to publish the event, eg:
@PostsEvent(type=StartDateChangedEvent.class, subscribers={"foos", "bars"})
public LocalDate getStartDate() { ... }
public void setStartDate(LocalDate dt) { ... }
where the "foos" and "bars" are - again - the collections of the publishing
object to be loaded into memory first.
Or, as a refinement:
@PostsEvent(type=StartDateChangedEvent.class,
subscribersPolicy=StartDateSubscribersPolicy.class)
public LocalDate getStartDate() { ... }
public void setStartDate(LocalDate dt) { ... }
would allow a strategy object to be specified to allow arbitrary subscriber
objects to be loaded.
~~~
I don't think any of this is too difficult to implement, but is it worth
it? Or is this subscriber loading hint basically telling me that the
approach is fundamentally flawed?
Opinions welcome
Thx
Dan