Skip to content
Stefan Meyer edited this page Jul 28, 2012 · 5 revisions

EntityTypes can be enhanced by adding services. Their usage is similar to the idea of duck typing, in that the existence of the service / behavior needs to be checked first. These services can be resolved by calling entityType.getService(IdentityService.class). This will return null if the service is not supported by the type. To register a service you add a registrar or an instance of the service to the subrepository. The services will be discovered and instatiated lazily. Usually you have just one instance so the type will be one parameter in the service's methods.

public interface IdentityService {
	public Serializable getId(EntityType<?> entityType, Object entity);
}

Here is an example of how to use the service:

Serializable getId(Object entity) {
	EntityType<?> entityType = entityTypeRepository.getEntityType(entity);
	IdentityService service = entityType.getService(IdentityService.class);
	Serializable id = service.getId(entityType,entity);
	return id;
}

To add a service to an EntityType you need to create an implementation of EntityTypeServiceFactory. getServiceClass returns the service's type and createService returns the instance if applicable to the entityType.

public interface EntityTypeServiceFactory
{
	public Object createService(EntityType entityType);
	public Class getServiceClass(EntityType entityType);
}

To add a service to an EntityType an instance of EntityTypeServiceFactory needs to be registered with the EntityTypeSubrepository:

<bean id="jsonRepository" class="...JsonEntityTypeRepository">
	<property name="entityTypeServiceFatories">
		<list>
			<bean class="IdentityServiceFactory"/>
		</list>
	</property>
</bean>

Clone this wiki locally