Re: Can I use Guice to discover all implementations of a type?

2009-10-15 Thread Renat Zubairov

I was just thinking, would it be enough just to know all Guice
services which implement a given interface?

On Oct 14, 6:13 pm, Brian Pontarelli  wrote:
> Java.net Commons has a scanning thingy that uses ASM. You could just  
> yank it out and fix it up a bit:
>
> https://java-net-commons.dev.java.net/source/browse/java-net-commons/...https://java-net-commons.dev.java.net/source/browse/java-net-commons/...https://java-net-commons.dev.java.net/source/browse/java-net-commons/...
>
> You'll have to wait an hour to view these links because Java.net is  
> the world's worst website, but the classes work nicely and I use them  
> in JCatapult on pretty large codebases.
>
> -bp
>
> On Oct 13, 2009, at 7:12 PM, je...@swank.ca wrote:
>
>
>
>
>
> > On Oct 13, 6:44 pm, Eelco Hillenius  wrote:
> >> Heh. Well, I didn't want to bring it up after this thread:http://
> >> groups.google.com/group/google-sitebricks/browse_thread/thread...
> >> :-)
>
> > Yeah, I'm not a fan of classpath scanning either. But I do like the
> > idea of having a smooth migration path from JSR-299 to Guice.
>
> >> Interesting... is that a nice way of saying that you believe web  
> >> beans
> >> got to be a design by committee child, or just that Guice would be
> >> better suited for some rare corner cases etc?
>
> > I like Guice, but I'm biased. I'd like to see a thorough comparison of
> > the two!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Multibinding + Collection types

2009-10-15 Thread Johannes Schneider

Hi,

I recently started to use the Multibinder - thanks for that work.
But now I find me writing that code several times:


Multibinder strategyMultibinder =
Multibinder.newSetBinder( binder(), ImageSelectionStrategy.class );

strategyMultibinder.addBinding().to( Impl1.class );
strategyMultibinder.addBinding().to( Impl2.class );

bind( new TypeLiteral>() {
} ).to( new TypeLiteral>() {
} );


This is necessary because I inject the strategies using a parameter of
type "Collection".
Could that not-so-readable part with the type literals be skipped
somehow? I think that is quite a common use case and maybe should be
added to the multibinder?


Sincerly,

Johannes

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Binding question

2009-10-15 Thread eric

I have a super basic problem:

public class StringBinding {
private String name;

@Inject
public StringBinding(String name){
this.name = name;
}

public void hi(){
System.out.println(this.name);
}
}

How can I let hi() print the name I specified with Guice?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread oakridge

Hi all,

I am trying to upgrade to Guice 2.0, but keep getting the following
error:

Guice provision errors:1) Error in custom provider,
com.google.inject.internal.ComputationException:
com.google.inject.internal.ComputationException:
com.google.inject.internal.cglib.core.CodeGenerationException:
java.lang.reflect.InvocationTargetException-->null 

I am unclear as to why this is occurring.  I am trying to wrap a
module used elsewhere in my code (rootModule), but only expose certain
classes.  After some research, I was found this issue, but am unsure
if it is related:

http://code.google.com/p/google-guice/issues/detail?id=343

My code looks like this where the error occurs:

public class WrapperModule extends AbstractModule {

protected void configure() {

final AbstractModule rootModule =
new RootModule();

//error occurs after this line
bind(iRobot.class).toProvider(new Provider() {
public iRobot get() {
return Guice.createInjector( rootModule).getInstance
(Robot.class);
}
}).in(Scopes.SINGLETON);



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread Julian Klein
Let me add that if I move code around as follows, I get this error:

Oct 15, 2009 11:00:58 AM com.google.inject.MessageProcessor visit
INFO: An exception was caught and reported. Message:
java.lang.ClassNotFoundException:
com.google.inject.internal.cglib.reflect.FastClass
com.google.inject.internal.ComputationException:
com.google.inject.internal.ComputationException:
com.google.inject.internal.cglib.core.CodeGenerationException:
java.lang.reflect.InvocationTargetException-->null


public class WrapperModule extends AbstractModule {

   protected void configure() {

   final AbstractModule rootModule =
   new RootModule();
   final Injector injector = Guice.createInjector( rootModule);

   //error occurs after this line
   bind(iRobot.class).toProvider(
>
> new Provider() {
>public iRobot get() {
>return injector.getInstance
> (Robot.class);
>}
>}).in(Scopes.SINGLETON);

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread Stuart McCulloch
2009/10/15 oakridge 

>
> Hi all,
>
> I am trying to upgrade to Guice 2.0, but keep getting the following
> error:
>
> Guice provision errors:1) Error in custom provider,
> com.google.inject.internal.ComputationException:
> com.google.inject.internal.ComputationException:
> com.google.inject.internal.cglib.core.CodeGenerationException:
> java.lang.reflect.InvocationTargetException-->null 
>
> I am unclear as to why this is occurring.  I am trying to wrap a
> module used elsewhere in my code (rootModule), but only expose certain
> classes.  After some research, I was found this issue, but am unsure
> if it is related:
>
> http://code.google.com/p/google-guice/issues/detail?id=343
>

one way to find out would be to try this patched jar:


http://peaberry.googlecode.com/svn/tags/1.1.1/lib/build/guice-customloader-20090412.jar

which is Guice 2.0 plus the suggested patch for 343


> My code looks like this where the error occurs:
>
> public class WrapperModule extends AbstractModule {
>
>protected void configure() {
>
>final AbstractModule rootModule =
>new RootModule();
>
>//error occurs after this line
>bind(iRobot.class).toProvider(new Provider() {
>public iRobot get() {
>return Guice.createInjector( rootModule).getInstance
> (Robot.class);
>}
>}).in(Scopes.SINGLETON);
>
> 
>
> >

-- 
Cheers, Stuart

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread oakridge

Stuart,

I tried the jar at 
http://peaberry.googlecode.com/svn/trunk/lib/build/guice-2.0-customloader.jar,
which appears to have fixed the issue.  I am wondering however if the
problem is more an indication of the misuse of Guice within my code
base.  Do my examples above indicate bad practice or an anti-pattern
of some sort?  Unfortunately, I cannot use anything but a GA release
of Guice so this will block my upgrade.

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Binding question

2009-10-15 Thread Stuart McCulloch
2009/10/14 eric 

>
> I have a super basic problem:
>
> public class StringBinding {
>private String name;
>
>@Inject
>public StringBinding(String name){
>this.name = name;
>}
>
>public void hi(){
>System.out.println(this.name);
>}
> }
>
> How can I let hi() print the name I specified with Guice?
>

you mean like this...?

public class StringBinding {
private String name;

@Inject
public StringBinding(@Named("person") String name) {
this.name = name;
}

public void hi() {
System.out.println(this.name);
}

public static void main(String[] args) {
Guice.createInjector(new AbstractModule() {
protected void configure() {

bindConstant().annotatedWith(Names.named("person")).to("Barney");
}
}).getInstance(StringBinding.class).hi();
}
}

you can also use "Names.bindProperties(binder, properties)" to bind a whole
load of constants at once


> >
>


-- 
Cheers, Stuart

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Multibinding + Collection types

2009-10-15 Thread je...@swank.ca



On Oct 15, 6:42 am, Johannes Schneider  wrote:
> This is necessary because I inject the strategies using a parameter of
> type "Collection".
> Could that not-so-readable part with the type literals be skipped
> somehow? I think that is quite a common use case and maybe should be
> added to the multibinder?

I think a your one-statement is the right fix here. If you write this
too often, you can encapsulate it by using the Types utility class to
compose the appropriate type objects:
  bindWildcardCollectionForSet(binder(),
ImageSelectionStrategy.class);

http://google-guice.googlecode.com/svn/tags/2.0/javadoc/com/google/inject/util/Types.html
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread Stuart McCulloch
2009/10/15 oakridge 

>
> Stuart,
>
> I tried the jar at
> http://peaberry.googlecode.com/svn/trunk/lib/build/guice-2.0-customloader.jar
> ,
> which appears to have fixed the issue.  I am wondering however if the
> problem is more an indication of the misuse of Guice within my code
> base.


Hi Julian,

I can't say with only a small fragment to go on - but you might want to look
at the new features in Guice 2:

   http://code.google.com/p/google-guice/wiki/Guice20

such as being able to override bindings (see Modules.override), private
modules (see PrivateModules) and
the ability to create child injectors that inherit bindings from their
parent (see Injector.createChildInjector).


> Do my examples above indicate bad practice or an anti-pattern of some sort?


well if your aim is to hide bindings then you might find you can remove a
lot of your own code by using
the built-in child injectors and private modules support - however, because
the patched jar solved your
problem that suggests you have a binding somewhere to a system type (such as
Runnable / Random)
which is the trigger for the problem described in 343

this means you'd get this exception even without your module wrapping -
unless you removed bindings
with system type keys, or you were able to use a different (non-system)
class for their binding keys :(
(the full stack trace hopefully directs you to the relevant binding, or at
least the module containing it)

Unfortunately, I cannot use anything but a GA release
> of Guice so this will block my upgrade.
>

I understand, my hope is this patch will be in the 2.1 maintenance release
(ETA end of 2009 I believe?)


> Thanks
> >

-- 
Cheers, Stuart

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Multibinding + Collection types

2009-10-15 Thread Stuart McCulloch
2009/10/15 je...@swank.ca 

> On Oct 15, 6:42 am, Johannes Schneider  wrote:
> > This is necessary because I inject the strategies using a parameter of
> > type "Collection".
> > Could that not-so-readable part with the type literals be skipped
> > somehow? I think that is quite a common use case and maybe should be
> > added to the multibinder?
>
> I think a your one-statement is the right fix here. If you write this
> too often, you can encapsulate it by using the Types utility class to
> compose the appropriate type objects:
>  bindWildcardCollectionForSet(binder(),
> ImageSelectionStrategy.class);
>
>
> http://google-guice.googlecode.com/svn/tags/2.0/javadoc/com/google/inject/util/Types.html
>

+1 for using the Types utility class, it's saved me from typing lots of
angle brackets!


> >

-- 
Cheers, Stuart

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Incomprehensible Errors When Upgrading to Guice 2.0

2009-10-15 Thread oakridge

Thanks Stuart.  I have tried to track down the source binding causing
the problem.  Unfortunately, I think errors are being swallowed
somewhere.  I see no Guice log statements in my console when running
in IntelliJ.  We use log4j.  I have to figure out how to get logging
output to work so I can track down the issue.  I'll report back if I
can resolve the problem.

Thanks.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Multibinding + Collection types

2009-10-15 Thread Johannes Schneider

Thanks for pointing at that class. Have missed it completely.

But now I have a generics related problem with the compiler. I can fix
it using casts but I'd like to understand my error: Could anyone give me
a hint why this code does not compile?

binder.bind( Key.get( Types.newParameterizedType( Collection.class,
Types.subtypeOf( Integer.class ) ) ) ).to( Key.get( Types.setOf(
Integer.class ) ) );


Sincerly,

Johannes

je...@swank.ca wrote:
> 
> 
> On Oct 15, 6:42 am, Johannes Schneider  wrote:
>> This is necessary because I inject the strategies using a parameter of
>> type "Collection".
>> Could that not-so-readable part with the type literals be skipped
>> somehow? I think that is quite a common use case and maybe should be
>> added to the multibinder?
> 
> I think a your one-statement is the right fix here. If you write this
> too often, you can encapsulate it by using the Types utility class to
> compose the appropriate type objects:
>   bindWildcardCollectionForSet(binder(),
> ImageSelectionStrategy.class);
> 
> http://google-guice.googlecode.com/svn/tags/2.0/javadoc/com/google/inject/util/Types.html
> > 

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Multibinding + Collection types

2009-10-15 Thread Johannes Schneider

Hmm,

I think that Key should always be cast - of course.

So
binder.bind( ( ( Key> ) Key.get(
Types.newParameterizedType( Collection.class, Types.subtypeOf(
Integer.class ) ) ) ) ).to( ( Key> ) Key.get( Types.setOf( Integer.class ) ) );

compiles...


Thanks


Johannes Schneider wrote:
> Thanks for pointing at that class. Have missed it completely.
> 
> But now I have a generics related problem with the compiler. I can fix
> it using casts but I'd like to understand my error: Could anyone give me
> a hint why this code does not compile?
> 
> binder.bind( Key.get( Types.newParameterizedType( Collection.class,
> Types.subtypeOf( Integer.class ) ) ) ).to( Key.get( Types.setOf(
> Integer.class ) ) );
> 
> 
> Sincerly,
> 
> Johannes
> 
> je...@swank.ca wrote:
>>
>> On Oct 15, 6:42 am, Johannes Schneider  wrote:
>>> This is necessary because I inject the strategies using a parameter of
>>> type "Collection".
>>> Could that not-so-readable part with the type literals be skipped
>>> somehow? I think that is quite a common use case and maybe should be
>>> added to the multibinder?
>> I think a your one-statement is the right fix here. If you write this
>> too often, you can encapsulate it by using the Types utility class to
>> compose the appropriate type objects:
>>   bindWildcardCollectionForSet(binder(),
>> ImageSelectionStrategy.class);
>>
>> http://google-guice.googlecode.com/svn/tags/2.0/javadoc/com/google/inject/util/Types.html
> 
> > 

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Unit Testing child Injector

2009-10-15 Thread Karthik Krishnan

Hi Guice Gurus,

I am gradually introducing DI withGuice 2.x in my application. I am
hit upon a problem for which I hope you might be able to help me.

public class Service {

  private final String serviceType;

 @Inject
 public Service(String serviceType) {
this.serviceType = serviceType;
 }

 @Inject
private Dao myDao;

// Setter and getter for Dao.

}

The service is called directly from the delegate which accesses the
injector configured using Guice's ServletContextListener. One of the
Module (ServiceModule) explicitly binds the string as follows
public class ClientTypeModule {
@Override
protected void configure() {
bind(String.class).toInstance("CRV_CLIENT_TYPE");
   }
}

My issue is that there can be other potential values for the
serviceType. To get around that, I thought I would use child injectors
in my code that invokes this instance.

public class Delegate {
@Inject
private Service service

// Settter for Service.

private final Injector injector;

@Inject
public class Delegate(Injector injector) {
  this.injector = injector;
}


public void callService()  throws DelegateException {
  Injector childInjector = injector.createChildInjector(new
ClientTypeModule()).injectMembers(this);
  try {
  service.doFoo();
} catch (DaoException de) {
  throw new DelegateException();
}

}
}

I find that  I am unable to mock Service instance to throw
DaoException to test exception handling as my mock is overridden by
child injector. Is there a work around for this that any one can
suggest? Any suggestion would be appreciated.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Can I use Guice to discover all implementations of a type?

2009-10-15 Thread John Logsdon
Firstly, thanks to everyone who took the time to answer my question and
provide a solution so quickly!

Actually, in the end I realised that for my Use Case (Find all implementing
classes for an interface at startup) that a perfectly good solution exists
already in Java 6 - java.util.ServiceLoader. It doesnt require any
additional jars and just does exactly what I want. On reflection I'm not
sure this kind of functionality should be in Guice anyway since it's
Discovery not DI. With all th ebad experience we've had with webbeans I'm
all for projects that just do the one thing very well!

We've also replaced Webbeans DI with Guice and we're now web-bean widows and
happy for it! Everything just works now and we can get on with solving the
problem we set out to in the first place!

Congrats for getting 330 approved. We're all in your camp now!

John

2009/10/15 Renat Zubairov 

>
> I was just thinking, would it be enough just to know all Guice
> services which implement a given interface?
>
> On Oct 14, 6:13 pm, Brian Pontarelli  wrote:
> > Java.net Commons has a scanning thingy that uses ASM. You could just
> > yank it out and fix it up a bit:
> >
> >
> https://java-net-commons.dev.java.net/source/browse/java-net-commons/...https://java-net-commons.dev.java.net/source/browse/java-net-commons/...https://java-net-commons.dev.java.net/source/browse/java-net-commons/.
> ..
> >
> > You'll have to wait an hour to view these links because Java.net is
> > the world's worst website, but the classes work nicely and I use them
> > in JCatapult on pretty large codebases.
> >
> > -bp
> >
> > On Oct 13, 2009, at 7:12 PM, je...@swank.ca wrote:
> >
> >
> >
> >
> >
> > > On Oct 13, 6:44 pm, Eelco Hillenius  wrote:
> > >> Heh. Well, I didn't want to bring it up after this thread:http://
> > >> groups.google.com/group/google-sitebricks/browse_thread/thread...
> > >> :-)
> >
> > > Yeah, I'm not a fan of classpath scanning either. But I do like the
> > > idea of having a smooth migration path from JSR-299 to Guice.
> >
> > >> Interesting... is that a nice way of saying that you believe web
> > >> beans
> > >> got to be a design by committee child, or just that Guice would be
> > >> better suited for some rare corner cases etc?
> >
> > > I like Guice, but I'm biased. I'd like to see a thorough comparison of
> > > the two!
> >
>


-- 
John Logsdon
CEO
NetDev Limited
+44 1273 773661
+44 7899 811245
http://www.netdev.co.uk
Registered in England and Wales
Company Number 04741258

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Can I use Guice to discover all implementations of a type?

2009-10-15 Thread Eelco Hillenius

> Actually, in the end I realised that for my Use Case (Find all implementing
> classes for an interface at startup) that a perfectly good solution exists
> already in Java 6 - java.util.ServiceLoader. It doesnt require any
> additional jars and just does exactly what I want. On reflection I'm not
> sure this kind of functionality should be in Guice anyway since it's
> Discovery not DI. With all th ebad experience we've had with webbeans I'm
> all for projects that just do the one thing very well!

Good it works for your use case. It would be nice to have a more
generic scanning though, so that we can support other cases as well
(like classes with a @Singleton annotation).

Eelco

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



To scope or not to scope?

2009-10-15 Thread Paul Blessing

I'm looking for some advice on whether scoping would be the best way
to handle this situation.

Let's say I have a Swing app that can interact with exactly one
database at a time, but may have more than one active database through
the application's lifecycle.  The active database gets set when the
user chooses which database to edit.  Most of the other application
functionality depends on the active database.

Currently the application has what amounts to a Singleton object that
holds a reference to the active database and the dependencies get a
reference to the active database by calling an
ActiveDatabase.getActiveDatabase() method (yuck).  I'm looking to
eventually replace this with some Guice magic.

My initial thought is to have a DatabaseScope which holds a reference
to the active database as well as caches instances of other objects
which are valid as long as said database remains the active database.
My question is, is this the best way to achieve what I'm going for
here?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Constructor with a Bind class and a dynamic parameter

2009-10-15 Thread sunweiss

Hi,

Whats the best way to create a car object in the following scenario

class CarImpl {
final int horses;
final Engine engine

public CarImpl(Engine e, int h) {...}

}


bind(Engine.class).to(DieselEngine.class);
bind(Car.class).to(CarImpl.class); //How can i tell my CarImpl to use
which int?

Another scenario, what if the DieselEngine's constructor looks
something like this:

class DieselEngine {
DieselEngine(int volume) {...}
}

I know, the above class structure might not be the best from an OO
perspective, im just trying to figure out the best way to use guice.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Constructor with a Bind class and a dynamic parameter

2009-10-15 Thread Gary Pampara

Have a look at assisted injections.

That will allow you to provide the required int value and let Guice
perform the remainder of the injections.

On Fri, Oct 16, 2009 at 7:02 AM, sunweiss  wrote:
>
> Hi,
>
> Whats the best way to create a car object in the following scenario
>
> class CarImpl {
> final int horses;
> final Engine engine
>
> public CarImpl(Engine e, int h) {...}
>
> }
>
>
> bind(Engine.class).to(DieselEngine.class);
> bind(Car.class).to(CarImpl.class); //How can i tell my CarImpl to use
> which int?
>
> Another scenario, what if the DieselEngine's constructor looks
> something like this:
>
> class DieselEngine {
> DieselEngine(int volume) {...}
> }
>
> I know, the above class structure might not be the best from an OO
> perspective, im just trying to figure out the best way to use guice.
>
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---



Re: Constructor with a Bind class and a dynamic parameter

2009-10-15 Thread Widen
I think the core concept is that guice will help you to create new instances
or build relationships between instances by modules that need you to
configure, we called this mechanism as 'Injection'. So you can bind whatever
int value in some Module classes, then the injector will pick it up when it
creates new instances for you.

On Fri, Oct 16, 2009 at 2:20 PM, Gary Pampara  wrote:

>
> Have a look at assisted injections.
>
> That will allow you to provide the required int value and let Guice
> perform the remainder of the injections.
>
> On Fri, Oct 16, 2009 at 7:02 AM, sunweiss  wrote:
> >
> > Hi,
> >
> > Whats the best way to create a car object in the following scenario
> >
> > class CarImpl {
> > final int horses;
> > final Engine engine
> >
> > public CarImpl(Engine e, int h) {...}
> >
> > }
> >
> >
> > bind(Engine.class).to(DieselEngine.class);
> > bind(Car.class).to(CarImpl.class); //How can i tell my CarImpl to use
> > which int?
> >
> > Another scenario, what if the DieselEngine's constructor looks
> > something like this:
> >
> > class DieselEngine {
> > DieselEngine(int volume) {...}
> > }
> >
> > I know, the above class structure might not be the best from an OO
> > perspective, im just trying to figure out the best way to use guice.
> >
> >
> > >
> >
>
> >
>


-- 
Chinese: http://www.jiakuan.net
English: http://www.widenhome.com

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~--~~~~--~~--~--~---