
Originally Posted by
rebornspirit
I'm having the following problem. In a previous post I asked about how 'smart' domain object should be. Rod informed me that it was a bad design having domain objects that are just mindless drones...
This anti-pattern is called Anemic Domain Model
So now I'm writing a new domain object with that information in mind and I'm having the following problem. Let me sketch the situation.
- When my domain object is generated a particular fields should be filled by a sequence number that is generated by another class thar provides this functionality.
So now I have two options:
- I generate this sequence number in my facade and set in manually on the domain object
- Or, I let the domain objects generate it manually upon creation, but by thoing that I need to create a dependency.
I think you have given a difficult example. With OR-mappers you don`t always have control on the structure of your objects and how id`s are created. I think in this case it would be better to keep the id-part outside of the domain object.
And another remark: it isn`t always a good thing to add all logic to that object. Sometimes it is better to seperate some aspects so you can group functionality and not data. Example: calculation of bonus for every employee-class. If you have a lot of such calculations, your classes are going to drown in the logic (they are going to be large) and the logic is scattered in a lot of classes (maintenance nightmare). In such cases it is better to seperate them.
An example of smarter domain objects:
Code:
interface Employee{
int calcBonus();
}
class Ceo implements Employee{
int calcBonus(){
return 2*car.price()+1*office.price();
}
}
class SimpleEmployee implements Employee{
int calcBonus(){
return yearsWorking()*100;
}
}
You can also use something like this:
Code:
class BonusCalculator implements EmployeeVisitor{
static int calc(Employee employee){
EmployeeVisitor v = new EmployeeVisitor();
employee.accepts(v);
return v.amount;
}
int amount;
void visit(Ceo c){
amount = 2*c.getCar().price()+1*c.getOffice.price();
}
void visit(SimpleEmployee e){
amount = e.yearsWorking()*100;
}
}