[CB 3.5] Access RC collection from Model layer? Or pass RC to model layer?

Looking for hours without an answer. Maybe this is just about preferences??

From an event handler I can call a model service and pass in arguments in the method as such:
userHandler.cfc

`
UsersService.updateAccountInfo(rc);
or…
UsersService.updateAccountInfo(rc.fName,rc.lName,…);

`

However, I can also call the method without passing arguments, updateAccountInfo(), and just access the RC collection directly:
usersService,cfc

`
property name=“controller” inject=“coldbox”;

public void function updateAccountInfo() {
var rc = controller.getRequestService().getContext().getCollection();
}

`

It seems that either way the data is in the RC collection any ways.
What is the best way to stay decoupled in an OO manner and why?

Yeah it all boils down to preference, and how you want / or if you want, to decouple the models from your application.

Ultimately, it is about preference, generally speaking, it is bad practice to deal directly with the request context or it’s request collection in your models. The rc is basically your form and url variables and your models shouldn’t really be tied to your form fields and query strings. You should always account for the fact that you might want to update account info from a web service, from an event gateway, or from a different client front-end like Flash. It’s handy down the road to have a clear separation between your business logic that does stuff, and the parts of the framework that deal with plumbing of HTTP.

You also want to have a clear API for your service methods documenting what they take in and what they send back. If your service method directly accesses the request context, it’s breaking encapsulation and the contract between it and your handlers is very loose.

Out of your options, I would suggest your second example where you pass rc.fName and rc.lName in as separate arguments. That is the most reusable and self-documented.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Either way.

I can see two things. 1) pass the entire rc and keep all the arguments in one variable in your method. easy. no need to write more if you pass more values. 2) pass each variable on its own. its more explicit. more “written out” and more transparent. you know exactly what the method will need to run correctly.

also, best practice with models is dont include session, application, client data.

read your question wrong.

i would not access the event collection inside your model. pass that data from the outside.

Seems everyone would agree to pass arguments to the method rather than access the context from within the method.

And from what I can gather, with the exception of compound object arguments, it best to make the method transparent in what individual arguments it’s expecting.
Without which you don’t know what a method needs from it’s API unless you read the code.

Thank you for everyone’s input.

Well this is also why it is good to comment your code, which would clearly indicate what is being expected from arguments (inputs) and what it returns (output).

There is no hard fast rule, that says you have to do passing. But decoupling the model means it becomes decoupled and able to be reused with out external dependencies, which is what everyone is basically hinting at. But it is ultimately your choice on what you want from the model or business logic.