Mark Bucknell
Mark Bucknell

Reputation: 447

Controller and model/service separation of logic

I understand that business logic belongs in a model and application logic belongs in a controller. Could someone please clarify what business logic does and does not include and what application logic does and doesn't include. Giving consideration to things like redirects, confirmation dialogs etc

e.g.

 /**
         * Removes a product
         * @param product
         */
        _this.remove = function(product){
            if(confirm('Are you sure you want to remove "' + product.product_name + '"?')){
                var idx = _this.currentProductsOrdered.indexOf(product);
                _this.currentProductsOrdered.splice(idx, 1);
            }
        };

or should I have the confirm dialog box in the controller and then call the remove method?

Upvotes: 0

Views: 48

Answers (1)

Mike Robinson
Mike Robinson

Reputation: 8945

"Unfortunately, the pragmatic answer is: 'it depends.'" The MVC Model is not necessarily "all that it's cracked-up to be."

Nevertheless, I suggest that you can meaningfully divide the problem along this line: "'User Interface' ... versus ... 'Not'"

For instance: "the entire exchange with the Gentle User, no matter what it is, and no matter what sort of technical machinations might be involved with executing it," probably falls under the auspices of "User Interface == Controller."

Eventually, the user will commit to an action. "Okay, the user really wants this product to go away. So Be It."

Now you are engaging the Model layer, and telling him, "Be It So!" The instruction being given to the Model layer is, at this point, "an imperative command."

(Of course, the Model might reply: "No! I refuse!" In which case you have to go back to the User Interface.)

Basically, as I see it, "Controller == User Interface," and "Model == Actually Affecting the Data." But: "it's a guideline." It's just not set in stone.

Upvotes: 1

Related Questions