Scenario:
In my application (which utilises an rich domain model, where the logic is in the model, not in the services) I have users. I create new users with a service
User newUser = userService.createNewUser("Hans Dampf");
or get them from the database
User oldUser = userDao.findByName("Hans Dampf");
Because in every call into my application I have direct access to an user object, I would like to use the user object as an entry point into my domain model.
Each user can have different types of Galleries, saved in an other table.
class User {
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private Set<Gallery> automatic = new HashSet<Gallery>();
}
I want to have a simple way to enable a specific gallery. So my API would look like:
User user = ... // creating or retriving user
user.enableMainGallery();
Inside this method it would be necassary to create a new gallery object and add it to the list of galleries. But how to create this new instance? Using a factory? That would require to inject the factory into the domain object (may be problematic).
public void enableAutomaticGallery() {
automatic.add(automaticFactory.createAutomaticGallery(this));
}
Or is my interface definition flawed? Should I define it in some other way, such that I don't have to inject the factory? How?