Re: Gin, SingleInstanceProvider?

2011-02-05 Thread Gal Dolber
Maybe this will clarify what I was looking for:
http://codereview.appspot.com/4128063/
I implemented the SingleProvider, here is the test(FooBar isn't a
singleton):


  public void testProviderInGinjector() {

ProviderFooBar fooProvider = injector.getFooBarProvider();

assertNotSame(fooProvider.get(), fooProvider.get());// A Provider
returns always new instances

  }

  public void testSingleProviderInGinjector() {

SingleProviderFooBar fooSingleProvider = injector
.getFooBarSingleProvider();

assertEquals(fooSingleProvider.get(), fooSingleProvider.get());// A
SingleProvider returns always the same instance

  }

On Thu, Feb 3, 2011 at 7:40 PM, Gal Dolber gal.dol...@gmail.com wrote:

 Thanks


 On Thu, Feb 3, 2011 at 7:02 PM, zixzigma zixzi...@gmail.com wrote:

 I am not sure if this helps,
 but there is a feature called Binding Annotations,
 you can use BindingAnnotation to differentiate the Provider used in Class
 A,
 from the one used in Class B.

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

  --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




 --
 Guit: Elegant, beautiful, modular and *production ready* gwt applications.

 http://code.google.com/p/guit/







-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-05 Thread PhilBeaudoin
Here is a simple way to do what you want:

public class LazyT {
  @Inject ProviderT provider;
  private T instance;
  public T get( ) {
if (instance == null) {
  instance = provider.get();
}
return instance;
  }
}

public class Main {
  @Inject LazyThing thingA;
  @Inject LazyThing thingB;
  Main() { }
}

And then this passes:

Main main = ginjector.getMain();
assert(main.thingA.get() == main.thingA.get());
assert(main.thingA.get() != main.thingB.get());

I have tested it with Gin trunk, but it needs the fix mentioned by
Thomas in:
http://code.google.com/p/google-gin/issues/detail?id=136

(Cross-posted in the issue.)

On Feb 5, 3:34 am, Gal Dolber gal.dol...@gmail.com wrote:
 Maybe this will clarify what I was looking 
 for:http://codereview.appspot.com/4128063/
 I implemented the SingleProvider, here is the test(FooBar isn't a
 singleton):

   public void testProviderInGinjector() {

     ProviderFooBar fooProvider = injector.getFooBarProvider();

     assertNotSame(fooProvider.get(), fooProvider.get());    // A Provider
 returns always new instances

   }

   public void testSingleProviderInGinjector() {

     SingleProviderFooBar fooSingleProvider = injector
 .getFooBarSingleProvider();

     assertEquals(fooSingleProvider.get(), fooSingleProvider.get());    // A
 SingleProvider returns always the same instance

   }









 On Thu, Feb 3, 2011 at 7:40 PM, Gal Dolber gal.dol...@gmail.com wrote:
  Thanks

  On Thu, Feb 3, 2011 at 7:02 PM, zixzigma zixzi...@gmail.com wrote:

  I am not sure if this helps,
  but there is a feature called Binding Annotations,
  you can use BindingAnnotation to differentiate the Provider used in Class
  A,
  from the one used in Class B.

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

   --
  You received this message because you are subscribed to the Google Groups
  Google Web Toolkit group.
  To post to this group, send email to google-web-toolkit@googlegroups.com.
  To unsubscribe from this group, send email to
  google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2Bunsubs
   cr...@googlegroups.com
  .
  For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.

  --
  Guit: Elegant, beautiful, modular and *production ready* gwt applications.

 http://code.google.com/p/guit/

 --
 Guit: Elegant, beautiful, modular and *production ready* gwt applications.

 http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
yes, it is totally unnecessary.

1- you can annotate your Foo clas with @Singleton annotation.

2- alternatively you can use bind(Foo.class).in(Singleton.class)
in your GIN Module configure() method.

now you can use your Provider as you normally would,
and it gives you the same Foo instance all the time.

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Gal Dolber
Yes, I know about singletons. This is not the case.
What I am talking about is delayed instantiation.
In most of my current use cases the binded object aren't singletons.

On Thu, Feb 3, 2011 at 5:03 AM, Ryan Mehregan ryan...@gmail.com wrote:

 yes, it is totally unnecessary.

 1- you can annotate your Foo clas with @Singleton annotation.

 2- alternatively you can use bind(Foo.class).in(Singleton.class)
 in your GIN Module configure() method.

 now you can use your Provider as you normally would,
 and it gives you the same Foo instance all the time.

 --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
using a Provider always result in delayed on-demand instantiation,
the object is not create unless you get() it.

however, if you don't bind it in Singleton scope,
this deferred on-demand instantiation will create a new instance every time 
you call get() on the Provider.
in cases where you only need one instance, you can bind your provider in 
Singleton scope.

please have a look at Guice documentation below: the comments section at the 
very end.
it is similar in GIN.

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

Ryan

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
if you do not want your objects to be in Singleton scope,
just use Provider in following fashion.
no configuration is necessary.

MyClass {

private final ProviderFoo fooProvider;

@Inject
public MyClass(ProviderFoo fooProvider){
this.fooProvider = fooProvider;
}

public void myMethod(){

Foo foo = fooProvider.get(); // this result in on-demand instantiatin of 
Foo.
 // when you inject ProviderFoo in 
the constructor,
 // it is not instantiated. when you 
call .get(), only at that point
// foo instance is created

}

}

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
if you do not want your objects to be in Singleton scope,
just use Provider in following fashion.
no configuration is necessary.

public class MyClass {

private final ProviderFoo fooProvider;

@Inject
public MyClass(ProviderFoo fooProvider){
this.fooProvider = fooProvider;
}

public void myMethod(){

Foo foo = fooProvider.get(); // this result in on-demand instantiatin of 
Foo.
  
   // when you inject ProviderFoo in the constructor,
 // it is not instantiated. when you 
call .get(), only at that point
// foo instance is created

}

}

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
if you do not want your objects to be in Singleton scope,
just use Provider in following fashion.
no configuration is necessary.

public class MyClass {

private final ProviderFoo fooProvider;

@Inject
public MyClass(ProviderFoo fooProvider){
this.fooProvider = fooProvider;
}

public void myMethod(){

Foo foo = fooProvider.get(); // this result in on-demand instantiatin of 
Foo.
  
  // injecting ProviderFoo in the 
constructor
 // does not result in instantiation 
of Foo.
  
 // later, when myMethod is invoked, 
only at that point
 /// with the call to .get() , foo 
instance is created

}

}

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Ryan Mehregan
if you do not want your objects to be in Singleton scope,
just use Provider in following fashion.
no configuration is necessary.

public class MyClass {

private final ProviderFoo fooProvider;

@Inject
public MyClass(ProviderFoo fooProvider){
this.fooProvider = fooProvider;
}

public void myMethod(){

Foo foo = fooProvider.get();  // this result in on-demand instantiation of 
Foo.
  
  // injecting ProviderFoo in the 
constructor
 // does not result in instantiation 
of Foo.
  
 // later, when myMethod is invoked, 
only at that point
 // with the call to .get() , foo 
instance is created.

}

}
Post replyReply to 
authorLink../../../d/msg/google-web-toolkit/t31WrRikkzY/BOLw1VWJr0UJ

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Gal Dolber
No idea why you answer 3 times the same, but please read again my question.
I am using providers. As I already imagined gin doesn't have what I need.
I was looking for other solutions people were using.

What I need is: A provider of a non-singleton class that return always the
same instance.

I am doing it by hand as I showed on my first post.
Anyone having the same problem? and a better way of solving it?

On Thu, Feb 3, 2011 at 6:40 AM, Ryan Mehregan ryan...@gmail.com wrote:

 if you do not want your objects to be in Singleton scope,
 just use Provider in following fashion.
 no configuration is necessary.

 public class MyClass {

 private final ProviderFoo fooProvider;

 @Inject
 public MyClass(ProviderFoo fooProvider){
 this.fooProvider = fooProvider;
 }

 public void myMethod(){

 Foo foo = fooProvider.get();  // this result in on-demand instantiation of
 Foo.

   // injecting ProviderFoo in the
 constructor
  // does not result in
 instantiation of Foo.

  // later, when myMethod is
 invoked, only at that point
  // with the call to .get() , foo
 instance is created.

 }

 }
 Post replyReply to 
 authorLinkhttp://../../../d/msg/google-web-toolkit/t31WrRikkzY/BOLw1VWJr0UJ

 --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread zixzigma
I am using the same technique as Ryan suggested.

Your comment : non-singleton class that return always the same instance
isn't a non-singleton class that always return the same instance in fact 
the very definition of Singleton ? : )

I have read that there is a difference between Singleton Design Pattern and 
Singleton Scope.
Singleton Scope = always return the same instance, is this wrong ?

your code:
the foo==null check in redundant,
because provider.get() does that for you.

and if you want to always return the same instance,
then bind it in Singleton scope.

Foo assertFoo() {
if (foo == null) {
foo = provider.get();
}
return foo;
}

You have made me confused now,
I like to see some thoughts on this as well.

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Gal Dolber
If I inject a provider on 2 different classes I expect them to return a
different instance, but each provider must return the same one.

class A {
@Inject
public ProviderC p; // p.get() returns always the same instance
}

class B {
@Inject
public ProviderC p; // p.get() returns always the same instance
}

But A.p.get() is not the same instance than B.p.get().

On Thu, Feb 3, 2011 at 5:54 PM, zixzigma zixzi...@gmail.com wrote:

 I am using the same technique as Ryan suggested.

 Your comment : non-singleton class that return always the same instance
 isn't a non-singleton class that always return the same instance in fact
 the very definition of Singleton ? : )

 I have read that there is a difference between Singleton Design Pattern and
 Singleton Scope.
 Singleton Scope = always return the same instance, is this wrong ?

 your code:
 the foo==null check in redundant,
 because provider.get() does that for you.

 and if you want to always return the same instance,
 then bind it in Singleton scope.

 Foo assertFoo() {
 if (foo == null) {
 foo = provider.get();
 }
 return foo;
 }

 You have made me confused now,
 I like to see some thoughts on this as well.

  --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Gal Dolber
Sorry, the example should be with SimpleInstanceProvider, not with
Provider

On Thu, Feb 3, 2011 at 6:05 PM, Gal Dolber gal.dol...@gmail.com wrote:

 If I inject a provider on 2 different classes I expect them to return a
 different instance, but each provider must return the same one.

 class A {
 @Inject
 public ProviderC p; // p.get() returns always the same instance
 }

 class B {
 @Inject
 public ProviderC p; // p.get() returns always the same instance
 }

 But A.p.get() is not the same instance than B.p.get().

 On Thu, Feb 3, 2011 at 5:54 PM, zixzigma zixzi...@gmail.com wrote:

 I am using the same technique as Ryan suggested.

 Your comment : non-singleton class that return always the same instance
 isn't a non-singleton class that always return the same instance in fact
 the very definition of Singleton ? : )

 I have read that there is a difference between Singleton Design Pattern
 and Singleton Scope.
 Singleton Scope = always return the same instance, is this wrong ?

 your code:
 the foo==null check in redundant,
 because provider.get() does that for you.

 and if you want to always return the same instance,
 then bind it in Singleton scope.

 Foo assertFoo() {
 if (foo == null) {
 foo = provider.get();
 }
 return foo;
 }

 You have made me confused now,
 I like to see some thoughts on this as well.

  --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




 --
 Guit: Elegant, beautiful, modular and *production ready* gwt applications.

 http://code.google.com/p/guit/







-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread zixzigma
I am not sure if this helps,
but there is a feature called Binding Annotations,
you can use BindingAnnotation to differentiate the Provider used in Class A,
from the one used in Class B.

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

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



Re: Gin, SingleInstanceProvider?

2011-02-03 Thread Gal Dolber
Thanks

On Thu, Feb 3, 2011 at 7:02 PM, zixzigma zixzi...@gmail.com wrote:

 I am not sure if this helps,
 but there is a feature called Binding Annotations,
 you can use BindingAnnotation to differentiate the Provider used in Class
 A,
 from the one used in Class B.

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

  --
 You received this message because you are subscribed to the Google Groups
 Google Web Toolkit group.
 To post to this group, send email to google-web-toolkit@googlegroups.com.
 To unsubscribe from this group, send email to
 google-web-toolkit+unsubscr...@googlegroups.comgoogle-web-toolkit%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/google-web-toolkit?hl=en.




-- 
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/

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