Anemic domain object?

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).

-------------Problems Reply------------

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
}

Category:dao Views:1 Time:2011-12-04

Related post

  • How to avoid Anemic Domain Models and maintain Separation of Concerns? 2008-10-22

    It seems that the decision to make your objects fully cognizant of their roles within the system, and still avoid having too many dependencies within the domain model on the database, and service layers? For example: Say that I've got an entity with

  • Techniques for dealing with anemic domain model 2009-03-04

    I've read some of the questions regarding anemic domain models and separation of concerns. What are the best techniques for performing/attaching domain logic on anemic domain objects? At my job, we have a pretty anemic model, and we're currently usin

  • Service Layer validation vs. Domain Object validation; potential "abuse" of Domain Objects? 2011-02-24

    I've seen lots of book and article examples saying to put validation code in your Service Layer. Keep the Domain Objects "dumb" (aka, pure POCO's) and handle all validation that a Domain Object might do in the Service Layer. The Service Layer is resp

  • Domain Objects and Value Objects - are they equal? 2011-05-02

    By looking to the example of a Domain Object into Zend Quickstart tutorial, and other examples considering a DAO/VO patterns, they both seem to be very similar. Can we deduce that to say "Value Object" is the same as to say "Domain Object" ? If not,

  • Should domain objects and simple JavaBeans be unit tested? 2008-09-21

    Should simple JavaBeans that have only simple getters and setters be unit tested?? What about Beans with some logic in getters and setters? --------------Solutions------------- You should not write tests which: Test the language or the IDE (i.e. auto

  • Anemic Domain Model: Pros/Cons 2008-11-03

    I would like to know what the pros and cons are for using an Anemic Domain Model (see link below). Fowler Article --------------Solutions------------- The pros: You can claim it's a domain model and brag to your developer friends and put it on your r

  • Email notifications - In the domain object or a service? 2009-05-29

    I'm looking for recommendations on how to approach the following design problem (using a fictitious example based on stackoverflow). I'd trying to avoid an anemic domain model and seek general "best-practice" advice for this type of case. Scenario: S

  • Anemic Domain Model's vs. Domain Model in a simple domain driven design 2009-07-21

    I recently read a post on "The Anemic Domain Model Pattern" which caught my attention. As I read through this I found that the Anemic Domain Model description applied to many of the projects that I have worked on and built. I never thought of this as

  • Spring and the anemic domain model 2009-08-20

    So, I've noticed that I definitely have a tendency to pattern my Spring/Hibernate stack objects like this: Foo controller makes a call to "FooService" FooService calls FooRepository.getById() method to get some Foos. FooRepository makes some Hibernat

  • Does an Anemic Domain Model mean you can't use utility/support classes as "helpers" for your domain model? 2009-08-24

    According to this definition, Fowler's concept of Anemic Domain Model is: a software domain model where the business logic is implemented outside the domain objects and With this pattern, logic is typically implemented in separate classes which trans

  • Single Responsibility Principle vs Anemic Domain Model anti-pattern 2009-09-09

    I'm in a project that takes the Single Responsibility Principle pretty seriously. We have a lot of small classes and things are quite simple. However, we have an anemic domain model - there is no behaviour in any of our model classes, they are just p

  • anemic domain model versus domain model 2009-11-26

    Being confused again after reading about this anti-pattern and the many concerns about it here on SO. If I have a domain model and capture the data that must be persisted in a data transfer object, does that make my domain model a wrapper around the

  • If you are forced to use an Anemic domain model, where do you put your business logic and calculated fields? 2009-12-19

    Our current O/RM tool does not really allow for rich domain models, so we are forced to utilize anemic (DTO) entities everywhere. This has worked fine, but I continue to struggle with where to put basic object-based business logic and calculated fiel

  • Avoiding anemic domain model - a real example 2010-05-18

    I am trying to understand Anemic Domain Models and why they are supposedly an anti-pattern. Here is a real world example. I have an Employee class, which has a ton of properties - name, gender, username, etc public class Employee { public string Name

  • Building of your Domain Objects? 2010-08-17

    I'm in the process of trying to learn Domain Driven design so i'm sure this will be the first of many questions (i can already think of at least a couple more but i don't want to fragment my questions). I'm using Dino Esposito's "Architecting Microso

  • Does Domain Driven Design require to implement the business logic outside the domain objects. 2010-12-29

    The model of the domain are my entities used as POCOs which means no base class, no interfaces around and no Attributes. So the business logic like validation rules must be outside of the entities. (Anemic Domain Model) Would this comply with Domain

  • ObjectContext aware entity in EF to avoid Anemic Domain Model 2011-01-29

    In Entity Framework, is it possible to make the framework inject the DbContext into each object (entity) that is attached to or retrieved from the Context? I'm an NHibernate guy and I know it is possible in NH, -- sorry if it is a stupid question in

  • Understanding domain objects/services 2011-04-08

    What are domain objects and domain services in software architecture and design? How are they different from Business Logic Layer? --------------Solutions------------- Different people use these terms in somewhat different ways, but here's my take: 1

  • Is this an anemic domain model? 2011-05-03

    I'm trying to build my first CRUD application, and I don't understand if I should use an object containing getters and setters separated. Considering that we have the Zend Framework Quick Start tutorial with a Model structure containing: Gateway Data

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)