You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jan Vissers <Ja...@cumquat.nl> on 2007/10/29 19:28:33 UTC

Mixing T5 and Guice --- best practices

Partially inspired by a question/some work by Leon Pennings (on this 
list), partially because of my interest in Guice - if been trying to 
test some stuff on how best to integrate Guice and T5. Before asking 
whether my approach is appropriate I would like to ask what your opinion 
is on the whole idea of mixing Guice and T5 IoC?



This is what I've done for the NumberGuess tutorial.

public interface IPlayGenerator {
    int generateRandomNumber();
}

public class PlayGeneratorImpl implements IPlayGenerator {
    public int generateRandomNumber() {
       Random random = new Random();
       return random.nextInt(10) + 1;
    }
}

//Guice stuff
public class PlayModule extends AbstractModule {
    @Override
    protected void configure() {
        
bind(IPlayGenerator.class).to(PlayGeneratorImpl.class).in(Scopes.SINGLETON);
    }
}

//Actual client service
public class NumberGuessPlayer {
    private final IPlayGenerator generator;

    //Guice Inject
    @Inject
    public NumberGuessPlayer(IPlayGenerator generator) {
        this.generator = generator;
    }

    public int play() {
        return generator.generateRandomNumber();
    }
}

//T5/Guice bootstrapping
public class AppModule {
    // Guice Injector instance
    private static final Injector injector;

    static {
        injector = Guice.createInjector(new Module[] { new PlayModule() });
    }

    public static NumberGuessPlayer buildNumberGuessPlayerFinder() {
        return injector.getInstance(NumberGuessPlayer.class);
    }
    ...
    ...
}

//Start page
public class Start {
    @InjectPage
    private Guess guess;

    /T5 Inject
    @Inject
    private NumberGuessPlayer player;

    Object onAction() {
       int target = player.play();

       guess.setup(target);

       return guess;
    }
}


Any comments, tips....?

-J

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Mixing T5 and Guice --- best practices

Posted by Howard Lewis Ship <hl...@gmail.com>.
Seems like you'll need access to the Registry, or at least and ObjectLocator
to make them play together (allow Guice beans to be injected into Tapestry
services/components).

A contribution to MasterObjectProvider could do the job.

Haven't looked into it in detail.

On 10/29/07, Jan Vissers <Ja...@cumquat.nl> wrote:
>
> Partially inspired by a question/some work by Leon Pennings (on this
> list), partially because of my interest in Guice - if been trying to
> test some stuff on how best to integrate Guice and T5. Before asking
> whether my approach is appropriate I would like to ask what your opinion
> is on the whole idea of mixing Guice and T5 IoC?
>
>
>
> This is what I've done for the NumberGuess tutorial.
>
> public interface IPlayGenerator {
>     int generateRandomNumber();
> }
>
> public class PlayGeneratorImpl implements IPlayGenerator {
>     public int generateRandomNumber() {
>        Random random = new Random();
>        return random.nextInt(10) + 1;
>     }
> }
>
> //Guice stuff
> public class PlayModule extends AbstractModule {
>     @Override
>     protected void configure() {
>
> bind(IPlayGenerator.class).to(PlayGeneratorImpl.class).in(Scopes.SINGLETON
> );
>     }
> }
>
> //Actual client service
> public class NumberGuessPlayer {
>     private final IPlayGenerator generator;
>
>     //Guice Inject
>     @Inject
>     public NumberGuessPlayer(IPlayGenerator generator) {
>         this.generator = generator;
>     }
>
>     public int play() {
>         return generator.generateRandomNumber();
>     }
> }
>
> //T5/Guice bootstrapping
> public class AppModule {
>     // Guice Injector instance
>     private static final Injector injector;
>
>     static {
>         injector = Guice.createInjector(new Module[] { new PlayModule()
> });
>     }
>
>     public static NumberGuessPlayer buildNumberGuessPlayerFinder() {
>         return injector.getInstance(NumberGuessPlayer.class);
>     }
>     ...
>     ...
> }
>
> //Start page
> public class Start {
>     @InjectPage
>     private Guess guess;
>
>     /T5 Inject
>     @Inject
>     private NumberGuessPlayer player;
>
>     Object onAction() {
>        int target = player.play();
>
>        guess.setup(target);
>
>        return guess;
>     }
> }
>
>
> Any comments, tips....?
>
> -J
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
Partner and Senior Architect at Feature50

Creator Apache Tapestry and Apache HiveMind

Re: Mixing T5 and Guice --- best practices

Posted by Alex Shneyderman <a....@gmail.com>.
On 10/29/07, Howard Lewis Ship <hl...@gmail.com> wrote:
> and lifecycle, the need for service contributions (something none of the
> other IoC containers seem to "get") ... but Guice pushes the spectrum of

Not exactly correct. They do not provide it out of the box :-( One can
do the same with
spring for example - that's what grails does - but of course tapestry
shines in the simplicity
of the setup. But I agree that contributions are a big thing and none
of the containers seem
to make it easy to contribute. Perhaps not enough Eclipse influence up
there - they are all
stuck with IDEA ? :-)

Alex.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Mixing T5 and Guice --- best practices

Posted by Howard Lewis Ship <hl...@gmail.com>.
Credit where credit is due: I've borrowed a lot of great ideas from Guice.
Initially, T5 IoC started as just a better HiveMind, but the influence of
Guice really pushed the envelope in a number of ways.  Guice, coming in new
and based on generics and annotations, had a large number of innovations.

We disagree on a number of crucial ideas including certain aspects of scope
and lifecycle, the need for service contributions (something none of the
other IoC containers seem to "get") ... but Guice pushes the spectrum of
finding matches based on type and annotations.

On 10/29/07, Thiago H de Paula Figueiredo <th...@gmail.com> wrote:
>
> On Mon, 29 Oct 2007 16:28:33 -0200, Jan Vissers <Ja...@cumquat.nl>
> wrote:
>
> > Partially inspired by a question/some work by Leon Pennings (on this
> > list), partially because of my interest in Guice -
>
> What exactly makes you interested in Guice? As far as I know (but I
> haven't taken a look in Guice for some time already, so I can be wrong),
> Tapestry-IoC does (almost) everything Guice does, but better. One of the
> reasons is that T-IoC needs no annotations in your beans. :)
>
> --
> Thiago H. de Paula Figueiredo
> Desenvolvedor, Instrutor e Consultor de Tecnologia
> Eteg Tecnologia da Informação Ltda.
> http://www.eteg.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
Partner and Senior Architect at Feature50

Creator Apache Tapestry and Apache HiveMind

Re: Mixing T5 and Guice --- best practices

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 29 Oct 2007 16:28:33 -0200, Jan Vissers <Ja...@cumquat.nl>  
wrote:

> Partially inspired by a question/some work by Leon Pennings (on this  
> list), partially because of my interest in Guice -

What exactly makes you interested in Guice? As far as I know (but I  
haven't taken a look in Guice for some time already, so I can be wrong),  
Tapestry-IoC does (almost) everything Guice does, but better. One of the  
reasons is that T-IoC needs no annotations in your beans. :)

-- 
Thiago H. de Paula Figueiredo
Desenvolvedor, Instrutor e Consultor de Tecnologia
Eteg Tecnologia da Informação Ltda.
http://www.eteg.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org