DEV Community

Yogesh Galav
Yogesh Galav

Posted on

Use MVCRSH instead of MVC.

There is a famous style of programming which most of you already knew "MVC" it's even inbuild in frameworks like Laravel and ROR.
You can also apply this style to frontend framework like flutter, Vue and React for better productivity.
Today I want to share an extended version of MVC which we all should use instead of MVC to keep code clean and healthy.
It's MVCRSH - Model, View, Controller, Routes, Services and Helpers.

What is MVC?

MVC (Model, View, Controller) is architecture for dividing our code in application.

Why we need to extend it?

MVC might be an good style for keeping things separated but there will be sometimes when you will get confused if the particular piece of code should be put in Controller or Model.

Model

Model should contain only definition of entity or database table, The field it contains, type of fields, foreign-key etc.

  1. To define our list of fields we can define an array or object. In array you can only suggest field names while in object you can also provide their data types.
  2. We can define relationship with help of functions to fetch related data, In most ORM we can define what kind of relationship do we want with other entities and fetch data accordingly.
  3. We can also define Accessor or Mutator to run some condition before fechting or saving data.
  4. Here's the list of famous ORM w.r.t frameworks which provide model functionality, for Laravel it's Eloquent, for Node it's Drizzle or prism, for Flutter it's Sqflite.

View

View should only contain UI part, which user sees or interact with. For web It ultimately compiles to HTML, CSS and JS.
Hence all the files related to it come inside view.

  1. We can define which layout do we wan to use.
  2. When using frontend framework, reactive data and js methods also comes into picture.
  3. Along with it, more complex things like frontend routing and state management start to fill the view part. With tools like Inertia or File based routing we can eliminate frontend routing code.
  4. Here's the list of famous templating engine w.r.t frameworks which provide UI functionality, for Laravel it's Blade, for Express it's pug, for Flutter it's Flutter.

Controller

Controller as we know should only contain business logic but there's lot of logic like validation, running big query, sending email, generating csv or excel and using services like Twilio, stripe, gmap etc.

  1. We should only call functions to above all use cases inside controller, the way we call model inside controller.
  2. Validation can be call before executing controller function like middleware. Framework like Laravel provide dependency injection for doing the same.
  3. Rest of the things will move to services and helpers.
  4. Controllers should contain only single word functions like create, update, delete, index, show, download, csv, excel etc

Routes

Routes as we all know are starting point of user entry in application, hence it's most simple and absolute one.
Most framework provide routing by default, In framework like flutter you can use get_router or getx to define routes.

Services

Services should be made in way, so that they can be used in any other project like ApiService, CsvExport, ExcelImport, TwilioSms, Mailchimp.

  1. The code in services should always be reusable so it can be used in any project.

Helpers

Last but not the least, Helpers provide help to controller and model. The logic or function which makes controller bulky should be moved to helpers instead of models. Most developers confuse between Utils and helpers but they are not the same.
In frontend frameworks, state management tools are kind of helpers only. In backend frameworks you need to create one otherwise you would end up writing most of the bulky logic inside controller or model.

  1. Helpers should contain only queries and operations on model to retrieve particular set of data.
  2. Helpers can call respective Models but not services or jobs.
  3. Models, Controllers and Helpers should have one to one relationship. For eg. PostController should call only PostModel and PostHelper.

I hope you find this post useful for whatever framework you use.
Thank you and have a nice day!

Top comments (1)

Collapse
 
sourovpal profile image
Sourov Pal

Hi,
This is Sourov Pal. I am a freelance web developer and Software Developer. I can do one of project for free. If you like my work you will pay me otherwise you don't need to pay. No upfront needed, no contract needed. If you want to outsource your work to me you may knock me.

My what's app no is: +8801919852044
Github Profile: github.com/sourovpal
Thanks