Reputation: 2430
When should I use command objects and when domain objects?
What are the disadvantages and advantages of each scheme to use?
Upvotes: 0
Views: 179
Reputation: 27255
When should I use command objects and when domain objects?
Domain objects are objects that you want to persist to the database. Command objects may be domain objects, but don't have to be. Any object could be used a command object. A command object is a convenient way to let the framework do a bunch of work for you (data binding, dependency injection, validation, etc...).
When you write a controller action like this:
class SomeController {
def someAction(SomeCommand co) {
// your code here...
}
}
The compiler will generate something like this (pseudocode, but representative):
class SomeController {
def someAction(SomeCommand co) {
// your code here...
}
def someAction() {
SomeCommand co = new SomeCommand()
bindData co, request
// subject co to dependency injection
co.validate()
someAction(co)
}
}
Upvotes: 2