My favorites | Sign in
Project Logo
                
Search
for
Updated Feb 07, 2009 by kuisong.tong
Exception  
Decouple modules using domain event bus

Introduction

In our previous system, there is a requirements: When deleting a object (eg. User), you need check other objects (eg. project) don't used it. But project and user are two modules, if we write code for this checking before save user, then user module and project module must dependent each other with a cycle dependency.

We must find a solution to decouple this two modules, event system is the best way to do it, but we don't want add Subject and Observer interface to our POJO implementation. Refer to Seam, we decide to use annotation to add subject and observer to POJO.

In sample, we use a simple case for this. We just validate the user's uniqueness.

Details

In Send mail after user created, we already raise a event when add a user, so we just need observer it such as:

@Component
public class UserValidator implements IUserValidator {
	@Autowired
	private UserRepository userRepository;
	
	public void validateUnique(UserDto user) {
		String userName = user.getUserName();
		if (userRepository.get(userName) != null) {
			throw new UserExistedException(userName);
		}
	}
}

interface IUserValidator {
	@Observer(value = "user.added", when = WhenEvent.BEFORE)
	void validateUnique(UserDto user);
}

Another interested part is UserExistedException, you can get localized message via getMessage(). The localized message is in exception.properties.


Sign in to add a comment
Hosted by Google Code