Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DomaEventHandler #15

Merged
merged 3 commits into from
Jul 26, 2017
Merged

Support DomaEventHandler #15

merged 3 commits into from
Jul 26, 2017

Conversation

making
Copy link
Member

@making making commented Jul 24, 2016

Pros and Cons compared to org.seasar.doma.jdbc.entity.EntityListener

Pros

  • POJO
  • Support multiple handlers on one entity
  • Support generic handler (ex. Audit Handler on all entities)
  • Entity does not need to know the name of listener class
  • Can also leverage Spring's @EventListener

Cons

  • Need to keep up with Doma
  • Ugly hack exists because @Entity with default parameters uses NullEntityListener and forces EntityListenerProvider to provide NullEntityListener instance.

Sample project

@making
Copy link
Member Author

making commented Jul 24, 2016

Comments are welcome!

@making making modified the milestone: 1.2.0 Jul 24, 2016
@nakamura-to
Copy link
Member

Great idea!
I think that it is more useful to make EntityListenerProvider implementation configureable with properties file.

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class DomaEventEntityListener<T> extends /* ugly hack */NullEntityListener<T>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to extend the NullEntityListener class?
You can implement the EntityListener interface directly.

@making making merged commit a019977 into master Jul 26, 2017
making added a commit that referenced this pull request Jul 26, 2017
@making
Copy link
Member Author

making commented Jul 26, 2017

Here are examples

@org.seasar.doma.Entity
public static class Entity {
}
@org.seasar.doma.Entity
public static class Entity2 {
}
@DomaEventHandler
static class PreInsertHandler {
Entity entity;
@HandlePreInsert
public void handlePreInsert(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PreInsertHandler2 {
Entity2 entity;
@HandlePreInsert
public void handlePreInsert(Entity2 entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PreInsertHandlerWithContext {
Entity entity;
PreInsertContext ctx;
@HandlePreInsert
public void handlePreInsert(Entity entity, PreInsertContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PreUpdateHandler {
Entity entity;
@HandlePreUpdate
public void handlePreUpdate(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PreUpdateHandlerWithContext {
Entity entity;
PreUpdateContext ctx;
@HandlePreUpdate
public void handlePreUpdate(Entity entity, PreUpdateContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PreDeleteHandler {
Entity entity;
@HandlePreDelete
public void handlePreDelete(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PreDeleteHandlerWithContext {
Entity entity;
PreDeleteContext ctx;
@HandlePreDelete
public void handlePreDelete(Entity entity, PreDeleteContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PostInsertHandler {
Entity entity;
@HandlePostInsert
public void handlePostInsert(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PostInsertHandlerWithContext {
Entity entity;
PostInsertContext ctx;
@HandlePostInsert
public void handlePostInsert(Entity entity, PostInsertContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PostUpdateHandler {
Entity entity;
@HandlePostUpdate
public void handlePostUpdate(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PostUpdateHandlerWithContext {
Entity entity;
PostUpdateContext ctx;
@HandlePostUpdate
public void handlePostUpdate(Entity entity, PostUpdateContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PostDeleteHandler {
Entity entity;
@HandlePostDelete
public void handlePostDelete(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class PostDeleteHandlerWithContext {
Entity entity;
PostDeleteContext ctx;
@HandlePostDelete
public void handlePostDelete(Entity entity, PostDeleteContext ctx) {
this.entity = entity;
this.ctx = ctx;
}
}
@DomaEventHandler
static class PrePostInsertHandler {
Entity preEntity;
Entity postEntity;
@HandlePreInsert
public void handlePreInsert(Entity entity) {
this.preEntity = entity;
}
@HandlePostInsert
public void handlePostInsert(Entity entity) {
this.postEntity = entity;
}
}
@DomaEventHandler
static class InsertHandler {
Entity entity;
@HandlePreInsert
@HandlePostInsert
public void handleInsert(Entity entity) {
this.entity = entity;
}
}
@DomaEventHandler
static class NoArgHandler {
@HandlePreInsert
public void noarg() {
}
}
static class SpringListener {
DomaEvent event;
@EventListener
public void listen(DomaEvent event) {
this.event = event;
}
}
@org.seasar.doma.Entity
public static class Todo {
String createdBy;
public String getCreatedBy() {
return createdBy;
}
}
static class TodoListener {
Todo todo;
@EventListener(condition = "#root.event.source.createdBy == 'making'")
public void handlePreInsert(PreInsertEvent<Todo> event) {
this.todo = event.getSource();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants