arquillian / continuous-enterprise-development

Testable Solutions for Modern Applications
http://continuousdev.org/
116 stars 150 forks source link

Add Lifecycle Events to Repositories #42

Closed aslakknutsen closed 11 years ago

aslakknutsen commented 11 years ago

Fire CDI Events on Create/Update/Delete

@Inject Event<T> event;

store(a) {
  event.fire(a)
}
aslakknutsen commented 11 years ago

Could be implemented as a Decorator. One problem is we don't separate between Create/Update in Repository.

package org.cedj.geekseek.domain;

import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Any;
import javax.inject.Inject;

import org.cedj.geekseek.domain.model.Identifiable;

@Decorator
public abstract class EventRepositoryDecorator<T extends Identifiable> implements Repository<T> {

    @Inject
    private Event<T> event;

    @Inject @Any @Delegate
    private Repository<T> delegate;

    @Override
    public T store(T entity) {
        T stored = delegate.store(entity);
        event.select(new Created.Literal()).fire(entity);
        return stored;
    }

    @Override
    public void remove(T entity) {
        delegate.remove(entity);
        event.select(new Removed.Literal()).fire(entity);
    }
}