Guice & Vaadin & Request

140 views
Skip to first unread message

Luca Miorelli

unread,
May 28, 2014, 10:56:27 AM5/28/14
to google...@googlegroups.com
Hi, I work with legacy web app that have a ojbect in session scope.
I create a new front end with Vaadin and mvp lite that work togheter with old Struts front end.

Now in my vaadin application class at every request, i create a new injector with an object because i retrieve a UserContextInfo object from Session that have some infos of user and db name to use.

    private Module createModule(final CreateViewService s) {
        Module modules = new AbstractModule() {

            @Override
            protected void configure() {
                bind(EventBus.class).toInstance(eventBus);
                // usercontext info get from request
                bind(UserContextInfoDecorator.class).toInstance(userContextInfo);
                bind(View.class).annotatedWith(ParentViewAnnotation.class).toInstance(s.getParent());
                bind(Parameter.class).toInstance(s.getParameters());
                bind(SecureValidator.class).toInstance(validator);
            }
        };

        return modules;
    }

At every request i create a new injector whit this method and catch the exact instance of UserContextInfo.

    private View createViewInstance(Class<? extends View> clazz, Module modules) throws GuiException, ProvisionException {
  
        Injector localInjector = Guice.createInjector(modules);
        View instance = localInjector.getInstance(clazz);

        instance.inizialize(validator);
        return instance;
    }

It right use of guice? or it's better to exclude instance parameter from injections? Any suggestion?

Nate Bauernfeind

unread,
May 28, 2014, 4:09:18 PM5/28/14
to google...@googlegroups.com
I think it's perfectly reasonable to use Guice this way. You may want to make sure that you're being careful about not leaking memory (i.e. it looks like your event bus is shared across calls, so you'll want to make sure things like that aren't holding on to the object graph). Also, if you use Guice outside of each request, you may consider using a childInjector to easily get global scope into wherever the request goes. I've personally wanted to try something like this to see if it can better prevent some terrible spaghetti code in a fairly large multi-tenant code base. You should write back in a couple of months how much you liked or disliked your design; I'd love to hear it.

Nate


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

Stephan Classen

unread,
May 28, 2014, 4:09:38 PM5/28/14
to google...@googlegroups.com
Creating the injector can be rather expensive.
You may consider either of the following:

- Create child injectors instead of the main injector for every request.

- Use RequestScope (see: https://code.google.com/p/google-guice/wiki/Scopes) to have an instance which is a singleton for the request and can be used to store the data which you collected from the request.

Luca Miorelli

unread,
Jun 4, 2014, 5:52:46 AM6/4/14
to google...@googlegroups.com
Thank you for all reply. But i have another question, is child injector lighter as normal injector?

Tim Boudreau

unread,
Jun 17, 2014, 9:45:02 PM6/17/14
to google...@googlegroups.com
At every request i create a new injector whit this method and catch the exact instance of UserContextInfo.

That's a little draconian - try using a custom scope instead - either roll your own if it's simple, or use something like this:  https://github.com/timboudreau/scopes

-Tim

David Sowerby

unread,
Jun 18, 2014, 10:53:00 AM6/18/14
to google...@googlegroups.com
Hi Luca

I would agree with Tim, a custom scope would seem to be a simpler approach.  If you are using Vaadin 7, in addition to the example Tim gives, you could take a look at https://github.com/davidsowerby/v7 which also contains a VaadinSessionScope).  
Reply all
Reply to author
Forward
0 new messages