MRalwasser
MRalwasser

Reputation: 15953

Guice: Scope related questions

I'd like to use a dependency injection framework.

During my evaluation I came to the conclusion that Google Guice seems to fit best for my demands.

However, some questions came into my mind:

  1. Imagine a web application in which a user can have independent windows within a http session. The Session scope is too general while the Request scope is too narrow for me.
    Is there a scope which will help me out? (something I would call "window" or "controller instance" scope)

  2. Are there any pitfalls writing a custom scope?

  3. Our web application and several stand alone console applications are using the same classes. I am facing the problem that the scope of a class depends on the application type which is only known at runtime.
    E.g. in a standalone application the scope would be "No-Scope" or "per-Thread Scope", while in a web application it would be bound to a Session/"window".
    How to solve this problem?

Upvotes: 2

Views: 752

Answers (3)

Sid Malani
Sid Malani

Reputation: 2116

You can use scope with application context handlers which help in deciding how your scoping logic works. Then using the same custom scope you can control how the objects get created at runtime.

Upvotes: 1

Leonard Brünings
Leonard Brünings

Reputation: 13222

To answer 3. use different modules for your versions, and set the scopes there.

bind(Grill.class).to(Applebees.class).in(Scopes.SINGLETON);

Upvotes: 2

Boris Pavlović
Boris Pavlović

Reputation: 64622

  1. You'll have to create a custom scope
  2. Not that I know. We've been using a custom scope and it works very well.
  3. Have a different implementations of the custom scope being used in the web application and other for the standalone application

Upvotes: 2

Related Questions