3

In my system, an user can publish any number of trips. Mi User class (domain object) is like this

public class User {
    private String name;
    private String id;
    /* More private fields */

    /* getters and setters */
}

So if I want to get all the trips of the user with id = 1:

/* Domain Layer */
public class UserManager {
    ...
    public Trip[] getAllTrips(int userId) {
        dao.getAllTrips(userId);
    }
    ...
 }

/* DAL Layer */
public class UserDaoImpl implements IUserDao {
    public Trip[] getAllTrips(int userId) {
        /* jdbc here */
    }
}

It works, but I think my User class suffers the 'anemic domain problem' (or the anemic POJO problem,does it exists?): only has private fields and 'getters' and 'setters' (and all my POJO's the same).

I've thought another approach:

public class User {
    /* More private fields */
    private Trip[] trips;

    /* getters and setters */
    public Trip[] getTrips() {
        return trips;
    }
    ...
    public void addTrip(Trip trip) {
        // add the trip
    }
}

And

public class UserManager {
    public Trip[] getAllTrips(int userId) {
        User user = dao.getUser(userId);
        return user.getTrips();
    }
 }

With this second approach the User class has more functionality but the trips are not stored in the database.

I am missing something? I'm newbie with DAO and I don't know if I'm taking the correct approach.

Thanks (yeah, my English sucks).

Marcos
  • 4,643
  • 7
  • 33
  • 60

1 Answers1

1

Why not also add the getAllTrips function to User class? As long as your function works on one user object, add functions to the User class.

UserManager class would make sense if you performed action on multiple users e.g.

cancelTrip(int tripId) 
{ 
    // remove trip from all users 
} 
Andrzej Bobak
  • 2,106
  • 3
  • 28
  • 36
  • Yes, the getAllTrips function in the User class make sense (and use an array for store them for example), but the main question is about the persistence mechanism: I think I should use a database to store the trips and not use a simple array (or similiar). – Marcos Dec 08 '11 at 22:23
  • I guess the question is how are you designing your system. It would make sense to have user objects, trip objects and a many to many relation between them. – Andrzej Bobak Dec 09 '11 at 10:47
  • Thanks, I already have those objects. But, for example, if I add the getAllTrips function to User class, the trips will have to be stored in a list or array, right? And then I'll have to copy the array or list to the database, right? So why not store the trips directly in the database using DAO (without the intermediate array)? I've never worked with databases and I'm little confused. – Marcos Dec 09 '11 at 16:11
  • Start with reading about relational databases. Imagine how would you like to store those data in a database. Design database tables containing the data and finally write classes reflecting those tables. After that all you need is read/write mechanisms (DAOs for example) – Andrzej Bobak Dec 13 '11 at 13:30