RequestFactory client-side inheritance of typed class with generics

后端 未结 1 571
抹茶落季
抹茶落季 2021-01-23 12:22

I would like to know, is it possible to use generic classes of request factory proxy/context for common actions for all entities, like getById(Long id).

In

相关标签:
1条回答
  • 2021-01-23 12:56

    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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题