Hi,

On Sun, Apr 21, 2013 at 11:12 PM, Andre Schütz <[email protected]> wrote:

> Hello,
>
> I have a question regarding MongoDB, Morphia, wicket-guice and injection.
> I try to understand the injection of a UserDAO class in an ExampleClass. I
> do not understand the following point:
>
> The UserDAO class extends BasicDAO. BasicDAO needs a constructor with a
> Datastore variable.
> Question: How does wicket-guice inject the UserDAO class when the
> constructor needs a Datastore?
>
> The project would look as follows:
>
> pom.xml
> ---------------------------
>
> wicket-guice
> <dependency>
>         <groupId>org.apache.wicket</groupId>
>         <artifactId>wicket-guice</artifactId>
>         <version>6.6.0</version>
> </dependency>
>
> User
> ---------------------------
>
> @Entity("users")
> public class user {
>   @Id
>   private ObjectID id;
>   private name;
>   [...]
> }
>
> public interface IUserDAO {
>
> }
>
> UserDAO
> ---------------------------
>
> public class userDAO extends BasicDAO<User, ObjectId> implements IUserDAO {
>   public UserDAO(Datastore ds) {
>     super(ds);
>   }
>   [...]
> }
>
> MongoResource
> ----------------------------
>
> public enum MongoResource {
>   INSTANCE;
>   private MongoClient mongoClient;
>
>   private MongoResource () {
>     mongoClient = new MongoClient(properties.getProperty("host",
> properties.getProperty("port")));
>   }
>
>   public Datastore getDatastore() {
>     return new
> Morphia().map(User.class).createDatastore(mongoClient,"mydb");
>   }
> }
>
> Application
> -----------------------------
>
> public class Application extends WebApplication {
>   @Override
>   protected void init()
>   {
>       addComponentInstantiationListener(
>         new GuiceComponentInjector(this, new GuiceModule()));
>   }
> }
>
> GuiceModule
> -----------------------------
>
> public class GuiceModule extends AbstractModule
> {
>     @Override
>     protected void configure()
>     {
>         // Business object bindings go here.
>
             Datastore ds = MongoResource.INSTANCE.getDatastore();
             bind(MyDAO.class).to(new UserDAO(ds));

i.e. you can bind to an instance if it is thread-safe, i.e. stateless

If you want a new instance of UserDAO (a prototype) for each usage then you
can bind the Datastore and inject it in UserDAO:

class UserDAO ... {
...
@Inject private Datastore ds;
...
}


    }
> }
>
> ExampleClass
> -----------------------------
>
> public ExampleClass {
>   @Inject
>   private IUserDAO userDAO;
>
>   public ExampleClass() {
>     super();
>     userDAO.doSomething();
>   }
> }
>
> Without injection I would initialize the UserDAO class as follows:
>
> ExampleClass
> -----------------------------
>
> public ExampleClass {
>   private UserDAO userDAO;
>
>   public ExampleClass() {
>     super();
>     Datastore ds = MongoResource.INSTANCE.getDatastore();
>     userDAO = new UserDAO(ds);
>     userDAO.doSomething();
>   }
> }
>
> I would like to use injection. Can someone tell me how I have to do that?
> That would be great.
>
> Andre
> --
> Andre Schütz <[email protected]>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>


-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Reply via email to