RequestFactory client-side inheritance of typed class with generics

百般思念 提交于 2019-12-02 07:35:13
Chris Hinshaw

Yes this will work for you but you need to have the @ExtraTypes annotation on your RequestContext for all the different types you will need.

I posted a full example a while back.

GWT polymorphic lists with @ExtraTypes

Edit

To get this to work you should make your generic requestcontext use generics. This is what I have done in the past and it works correctly for me. You won't need the extra types on the generic to make this work because you are going to be telling it the type.

@Service(value = GenericDao.class, locator = MyServiceLocator.class)
public interface GenericContext<T extends GenericProxy> extends RequestContext {
    Request<T> getBy(Long id);
    Request<List<T>> get();
    Request<Void> save(T entity);
}


@Service(value = GenericDictionaryDao.class, locator = MyServiceLocator.class)
@ExtraTypes( {
    GenericDictionaryProxy.class,
    PageIProxy.class,
    ContentIProxy.class
    } )
public interface GenericDictionaryContext extends GenericContext<GenericDictionaryProxy> {
    public Request<List<GenericDictionaryProxy>> getListOrderedByName();

    // These used to be required due to a bug a while ago. Test without it
    // but if you get a method about an unknown method then this is the issue.
    Request<GenericDictionaryProxy> getBy(Long id);
    Request<List<GenericDictionaryProxy>> get();
    Request<Void> save(T entity);
}

There was a problem I found a while ago, not sure if it has been fixed but I also had to add the methods to the extending class. I didn't mind because I was still allowed to use my GenericContext when needed and everything worked. This allowed me to create a nice entity caching mechanism with guava LoadingCache.

Quick example.

public class EntityCache<T extends GenericProxy, R extends GenericContext<T>>  {

    private R requestContext;

    public EntityCache(R requestContext) {
        this.requestContext = requestContext;
    }

    public T get(Long key) {
       // get from loading cache but this is a simple example.
       requestContext.get(key);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!