I need brains here! :p
I have an interface IDAO< T ,K >, T is the entity class and K the primary key class.
I have developped a concrete class GenericDAO< T ,K >, that provides CRUD operations and Criteria Queries with automatic fetches (I have created an aspect @Joinfetch I use on entitiy relations). I can show the code if you want but it's not the subject :).
Here the Factory that produces GenericDAOs:
public class GenericDAOProducer {
@PersistenceContext
EntityManager em;
@Produces
@Default
public <T, K> IDAO<T, K> producesGenericDAO(
final InjectionPoint injectionPoint) {
final ParameterizedType type = (ParameterizedType) injectionPoint
.getType();
// Because of the new, i got to inject the class and the entity manager
// manualy
final IDAO<T,K> dao = new GenericDAO<T, K>(this.em,
(Class) type.getActualTypeArguments()[0]);
dao.init(); //cool stuff here to prepare some criteria queries
return dao;
}
}
Sometimes I need another implementations for DAOs (for complex fetching for exemple). Those implementations expose their methods by the IDAO façade too. I want those DAOs to be injected the same way that the GenericDAO.
Here is the feature that I want in an exemple:
I need an injection of an implementation of IDAO< Person, Long> somewhere. If exists a class implements IDAO< Person, Long> then I want CDI to choose this implementation. Else if no implementation exist for IDAO< Person, Long> I want CDI to choose the GenericDAO< Person, Long > produced by the Factory.
It's quite difficult for me to explain that in english. I hope you will understand my problem.
Do you have best practices to do that?
Thank you!