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.
        bind(MyDAO.class).to(UserDAO.class);
    }
}

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 <schuetz.an...@gmx.net>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to