Can DMI support generic DMI class ?

The following code seems can’t work (I use SmartClient 5.7.1)

public abstract class AbstractBean {
}

public class MyBean extendsAbstractBean {
}

public abstract class AbstractDMI<T extends AbstractBean> {
public List<T> fetch(T obj);
public T update(T obj);
public T remove(T obj);
public T add(T obj);
}

public class MyDMI extends AbstractDMI<MyBean> {
}

Need any extra thing to do to make it work ?

We don’t know of problems with using generics - can you show your .ds.xml file with the DMI binding? Possibly you have a typo.

I use SmartClient 5.7.1 + Jdk_6.0, to try “Spring + DMI + Custom DataSource”

1) applicationContext.xml

    <bean id="supplyItemDMI" class="com.zero.demo.service.SupplyItemDMIZj">
        <property name="dao" ref="supplyDao" />
    </bean>	

2) .ds.xml
<DataSource ID="supplyItemDMI" serverType="generic">
	<fields>
		<field name="itemID" type="sequence" primaryKey="true" hidden="true"/>
		<field name="itemName" type="text" title="Item Name" length="128" required="true"/>
		<field name="inStock" type="boolean" title="In Stock"  hidden="false"/>
	</fields>

	<serverObject lookupStyle="spring" bean="supplyItemDMI" />
</DataSource>

3)
public class BaseDMI<T extends AbstractEntity> {
    private BaseDao<T> dao;

    public List<T> fetch(T criteria, DSRequest dsRequest) throws Exception {
        return dao.fetch(criteria);
    }

    public T add(T record) throws Exception {
        return dao.add(record);
    }

    public T update(T record) throws Exception {
        return dao.update(record);
    }

    public T remove(T record) throws Exception {
        return dao.remove(record);
    }

    public void setDao(BaseDao<T> dao) {
        this.dao = dao;
    }
}

public class SupplyItemDMIZj extends BaseDMI<SupplyItem> {
}

4) dao
public interface BaseDao<T extends AbstractEntity> {
    List<T> fetch(T item);

    T add(T item);

    T remove(T item);

    T update(T item);

    T fetchEntityByID(T item);
}

public interface SupplyItemDao extends BaseDao<SupplyItem> {
}

public class SupplyItemDaoImpl extends AbstractDao implements SupplyItemDao {
}

When I click fetch button to click:

error messsage is like the following:

Unable to assign a required or optional argument to slot #1 taking type:
com.zero.demo.service.domain.AbstractEntity of method: public com.isomorphic.datasource.DSResponse com.zero.demo.service.BaseDMI.fetch(com.zero.demo.service.domain.AbstractEntity,com.isomorphic.datasource.DSRequest) throws java.lang.Exception
No remaining optional arguments match this type and all required arguments passed by the client have already been assigned.

Hi Louisling,

This looks like a Java limitation.

The method signature as inspected via Java Reflection in this case contains an Abstract type (com.zero.demo.service.domain.AbstractEntity) instead of the concrete type (SupplyItem) you supplied via Java generics. It appears there is no way for SmartClient to discover the concrete type of the first parameter via reflection, and it’s not possible to create an instance of an abstract type, so SmartClient can’t fulfill that argument.

If you instead declare your method as taking a DSRequest, everything will work normally. Note that, while this approach of using generics is pretty nifty, there is request metadata in the DSRequest that your final application will probably require anyway, such as dsRequest.oldValues for long transactions, and dsRequest.startRow and endRow for paging.

Thank you very much for your reply.
Yes, it’s really impossible create an instance of an abstract type.
DSRequest can work,
I just want to write a universal DMI, as I found the logic of all the DMI are much like, such as paging.

Hi Loiusling,

Creating a general purpose DMI is a good idea and I think you are on the right track using Java generics. You might take a look at the source code for the HibernateDataSource in the SDk. This is an implementation of a custom DataSource for Hibernate, which solves many of the same problems.

Thank you so much for your reply and guide.
The information you provided is really valuable.