Where are the Business Rules in MVC

10,859

Solution 1

At first brush, I'd say they belong in the model. The MVC Entry on Wikipedia seems to agree: "In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data". After all, by 'Business rules' we mean the functional algorithms and logic that encode the domain that your application is involved with, as opposed to input/output related logic. These core business-related logic does not - or should not- change based upon what is being displayed to the user (which is the domain of the View) or the user input (which is primarily received by the Controller).

In my experience, asking this sort of question has been very revealing during the software development process: we found a large number of things that were considered 'business rules' by some people, but turned out to be something else. If it is not a true business rule, it might not belong to the model.

Solution 2

The reason you never see MVC address "Business Rules" is that MVC by and large is a presentation pattern. It's focused on how to structure your application. The Model, for example, can be thought of as a presentation model. The model of your application, which the view then renders.

However, in order to create the presentation model, you generally need to go to your domain models where all your business logic lives. At that point, MVC doesn't dictate where that code physically live. Is it on another tier? MVC doesn't care.

Solution 3

Business rules always live in the model. The model is the bit that you could resuse with a completely different UI. The view is obviously completely dependent on UI choices and the controller has to take data from the model and tell the view to render it.

Putting business logic into the view is bad because it ties the structure to the presentation.

Putting business logic into the controller is bad because it splits your business domain between the data persisted by the model and the rules in the controller.

Solution 4

A quote from a Wikipedia Article:

MVC is often seen in web applications, where the view is the actual HTML page, and the controller is the code that gathers dynamic data and generates the content within the HTML. Finally, the model is represented by the actual content, usually stored in a database or in XML nodes, and the business rules that transform that content based on user actions.

Solution 5

Is there any reason why you cant mix MVC and Ntier? Our application does just that. Our controllers are used for data validation and decide which Business Layer calls to make.

OurApp.Web - Asp.net MVC Project
OurApp.Business - Business Layer Library
OurApp.DataAccess - Data Layer Library
OurApp.Entities - Basically all the 'models' shared by all layers

Share:
10,859

Related videos on Youtube

Scott and the Dev Team
Author by

Scott and the Dev Team

I was very young at birth, but have progressively aged since. I failed my way into software development, and I am very grateful to the medium that saved me from a lifetime of busing tables. I currently develop custom internet based productivity software.In 1984 I was voted "Most Likely to Fail Miserably" by the most cherished members of my extended family. Now I have the habit of waving my paychecks in their collective face at family get-togethers. I am not bitter, just happy.

Updated on August 11, 2020

Comments

  • Scott and the Dev Team
    Scott and the Dev Team almost 4 years

    Now that everyone is talking about MVC, I notice that the business rules are not being addressed. In the old days of the 3-tier architecture, The business rules were in the middle layer. Where do they fall in the new MVC?

    • Mauricio Scheffer
      Mauricio Scheffer over 15 years
      So? Quicksort is 46 years old and still being used. The important thing is that it works very well.
    • Bill the Lizard
      Bill the Lizard over 15 years
      I noticed the same thing a few years ago. It seemed like as soon as I learned it, it started popping up everywhere I looked.
  • Michael Dillon
    Michael Dillon over 14 years
    This comment is just plain WRONG. The controller is a technical part of the application that orchestrates the data flows and triggers actions in the model and the view. In a typical PC application the controller is concerned with mouse events, keystrokes and similar types of things.
  • Michael Dillon
    Michael Dillon over 14 years
    I think that a lot of people implicitly think of the model as the "database model" which is why business rules don't seem to have a place. If instead, you see the model as a simulation of something from the real world, then it is obvious that the business rules are as much a part of the model as the data that they manipulate.
  • Koobz
    Koobz about 14 years
    I like this approach. Unfortunately, I think many web frameworks make this leap an uncomfortable. At some point you might need helper objects in your business layer that don't necessarily correspond to any database tables / persistent storage - something that every model class I've seen seems to believe is a foundational thing. At that point you wonder, where well do I put this code?
  • Bernard Igiri
    Bernard Igiri almost 13 years
    That is not MVC. MVC is about organizing code. All dynamic webpages would be MVC by your definition.
  • Tae-Sung Shin
    Tae-Sung Shin over 11 years
    I believe OP is talking about business logic which belongs to model.
  • Andy
    Andy about 10 years
    The model though should NOT be in the MVC project and it should not have dependencies on any particular UI technology.