fmgasparino / google-gin

Automatically exported from code.google.com/p/google-gin
Apache License 2.0
0 stars 0 forks source link

GinFactoryModuleBuilder does not respect @Singleton #185

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In my application, I have a custom factory for activities.

public interface ActivityFactory {
  UserEditActivity getUserEditActivity(EntityProxy<UserProxy> id);
  UserSearchActivity getUserSearchActivity();
}

UserEditActivity uses assisted injection and a new instance should be returned 
on every getUserEditActivity() call.

UserSearchActivity is marked as @Singleton because I want to keep that activity 
state all the time (search results).

But a call to getUserSearchActivity() always return a new instance.

I could put the getUserSearchActivity() method directly on my GInjector (which 
would always return the same instance) but I don't like it that much, I prefer 
keeping all my activities factory methods in the same interface regardless of 
their scope.

Original issue reported on code.google.com by xavier.d...@gmail.com on 9 Apr 2013 at 2:49

GoogleCodeExporter commented 9 years ago
AFAICT, this is how Guice's AssistedInject work too: it doesn't care which 
scope you use, and a no-arg method is no different than any other.

BTW, rather than adding a method to the Ginjector, you'd probably rather inject 
a Provider<UserSearchActivity>.

Original comment by t.broyer on 9 Apr 2013 at 2:58

GoogleCodeExporter commented 9 years ago
I was hoping I could retrieve all my activities from the same factory 
regardless of their scope or their assisted/non-assisted nature but I think I'm 
asking too much.

You're right, I can use a Provider for all my non-assisted activities so I can 
make them singleton or not without changing anything else. It's not as perfect 
as I wanted but certainly good enough.

Original comment by xavier.d...@gmail.com on 9 Apr 2013 at 5:20

GoogleCodeExporter commented 9 years ago
This is indeed working as intended and Providers are the workaround you're 
looking for. AssistedInject does not support scopes and it should be an error 
to have scope annotations on assisted types or factories (right now they're 
just ignored).

Original comment by aragos on 10 Apr 2013 at 1:56