BeanFactory - Use in handler or service?

Using CB 2.6 DI on CF9
I understand that I can use the beanFactory to get and populate a bean
from the controller or the model but not sure what's best practice.

Example: a form submission to edit user data:
Do I use the handler controller to grab the bean, populate it, then
have the bean save itself (active record object), thus never touching
any services or model layer?
Or do I use a service in the model layer with beanFactory injected,
that grabs the bean, populates it using the rc struct gotten from the
requestContext, and has the bean save itself there?

Or is there something else altogether? Ideas?

Personally, my handlers will get a bean from a service class (there's often more to construction than *just* what the DI engine does), so my controller will be something like:

user =
(or user = UserService.get(form.userId) for update)
if(user.isValid()) {
else {
  redisplay form

So I use service layer as factory.

And bear in mind, the business object itself is part of the model, so you're definitely dealing with the model - just not necessarily with a service class.

Best Wishes,

You are likely to get a number of different opinions on this question, all of which are “right” in their own way. I’ll be the first to offer up mine. :wink:

Given the two choices that you laid out, I would opt for injecting the BeanFactory into a service object.

That said, I actually personally much prefer having a populate() method on my object that handles this task. So in my application, it looks something like so (pseudo-code)…

// controller
myFoo = getService().createFoo(args=event.getCollection());

// service
foo = entityNew(“Foo”);
return foo;

Thanks for the input guys. I've decided to use the beanfactory
injected into the service.
public void function doUpdateUser(event){
service: (controller & beanfactory injected)
public void function updateUser(){
//either get an object from the rc or get one from the beanfactory
rc = controller.getRequestService().getContext().getCollection();
beanFactory.populateBean(rc.oUser); //oUser is a session persisted
Only thing missing is validation in the service, ie. isValid =
ValidationService.validate("rc.oUser"); to check before doing the
object save() method. I have another topic on that started about the
validator plugin and Hyrule, take a look at it.