Business Process and Data with MVC (Model-View-Controller)

I have been thinking about the general issues with the MVC and the Vespa concept “Vespa: A better MVC”. The idea of splitting the Model component has really helped me a lot with a project that I am working on.

Let me emphasise that this doesn’t define how the model-view-controller (MVC) design pattern is supposed to be used. It merely suggests a slight alternative which allows the Model component to be split into business process and business data.

I decided to split Model into Service and Model (yes I stuck to the same name, but it has a lesser more specific goal). In my project a Service represents a business process whereas a Model only represents business data. I allow the Model to access data via an encapsulated PDO connection.

Plural Model classes are used to access singular Model representations. For example, a user ‘Bob’ can be retrieved via a service which will in turn utilise the UsersModel class.

The following diagram shows the classes and I have labelled the dependancy arrows to offer an idea of the process.

Service MVC Class Diagram with Process

Here are some rules that I am considering for my project:

  • Controller objects can load and utilise all Service and Model methods and fields.
  • Controller objects cannot instantiate a Model independently. This is a business process and must thus be undertaken by a Service. A business process could potentially decide to use an alternative model under certain situations.
  • Model and Service objects may interact with a data source.
  • Model objects must conform to the interface dictated by the corresponding Service class(es).
  • Model and Service objects can be overridden or adapted.
  • Service objects can be accessed by any View, Controller, Service or Model.

The relationship between Model and Service is many-to-many. A service can utilise many data models, and a data model can be processed and interpreted by many services.

I hope that you have enjoyed this article. Please feel free to leave comments!


2 thoughts on “Business Process and Data with MVC (Model-View-Controller)

    1. Hello Alex

      Apologies that it has taken me a little while to get back to you! Let’s use a simple user registration system as an example:

      • User (model): A class which represents the data for a single user.
      • User (service): A class which can create, modify, remove or operate on a user model. This would typically include processing SQL commands.
      • User Registration (service): A class which takes care of user registration and can consume the user service to register a new user. The user registration service might also want to utilise other services for roles and permissions.
      • Sign Up (view): A class which generates html/javascript/css allowing a user to read and input details. The view must not use a service, but it can read data from models which are provided by the controller.
      • User Registration (controller): The HTTP handler which selects the user registration view and service. You might have multiple registration services and views each with specialised goals (premium registration, free registration). The controller selects a service, invokes the relevant operation, selects a view, and then passes any relevant model data to the view.

      The idea is that each component should be decoupled from one another making it easier to mix, match and replace individual components as needed. In the above example it would be relatively easy to introduce a special type of registration page, or to access user information from another service (like an article service).

      I hope that this simple example is of help to you 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s