How can I create a single common factory for hundreds of service-interfaces?
I have a common generic super-interface, which all my service-interfaces extend: BaseDao<T>
There are hundreds of (generated) interfaces sub-classing my BaseDao
, e.g. CustomerDao extends BaseDao<Customer>
. Of course, I do not want to implement a single factory for every sub-class. Especially, because there is already a DaoFactory
, which I need to "glue" into my Weld-environment.
Hence, I implemented this:
@ApplicationScoped
public class InjectingDaoFactory {
@SuppressWarnings("rawtypes") // We *MUST* *NOT* declare a wild-card -- Weld does not accept it => omit the type argument completely.
@Produces
public BaseDao getDao(final InjectionPoint injectionPoint) {
final Type type = injectionPoint.getType();
// ... some checks and helpful exceptions ...
final Class<?> c = (Class<?>) type;
// ... more checks and helpful exceptions ...
@SuppressWarnings("unchecked")
final Class<BaseDao<?>> clazz = (Class<BaseDao<?>>) c;
final BaseDao<?> dao = DaoFactory.getDao(clazz);
return dao;
}
}
In the code requiring such a DAO, I now tried this:
@Inject
private CustomerDao customerDao;
But I get the error org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDao with qualifiers @Default
-- Weld does not understand that my InjectingDaoFactory
is capable of providing the correct sub-class to meet the dependency on CustomerDao
.
Please note that I (of course) did not have the chance to debug the code of my factory. Maybe I need to use InjectionPoint.getMember()
instead of InjectionPoint.getType()
-- this is not my problem, now. My problem is that the responsibility of my factory for the sub-interfaces extending BaseDao
is not understood by Weld at all.
So, what do I need to do to make Weld understand that one single factory can provide all the implementations of the many sub-interfaces of my BaseDao
common DAO-interface?