Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ORM with a strict MVC Framework

I am pretty new to ColdFusion ORM and I have some questions that I was hoping to get help with.

I am working on a framework that utilizes a pretty standard and strict MVC architecture. This framework also has a "service layer" that sits in between the controller and the model. The service layer is handling all of the business logic and validation.

A standard CRUD process goes like this:

user submits a form through the view --> view submits to the controller --> controller sends to the service layer for processing --> the service layer can perform any sort of business logic and validation to the request --> the service layer then sends the data to the model --> the model runs a transaction with an EntityNew() and a EntitySave() --> the model then sends back a response with an ID back to the service layer --> the service layer applies some logic and builds a message for the user --> the service layer than sends the response message to the controller --> the controller then sends it to the view --> the view then displays it to the user.

This process works good and I am happy with the separation of roles. It keeps all of the logic away from the view, controller and model and isolates it in the service layer.

My question is when I need to communicate with the database at the service layer for things like checking if a username is unique for validation how should I do that? Right now I am just running a EntityLoad() right in the service layer. This seems to technically go around the model layer and interfaces directly with the database by way of ORM. Would this be considered breaking the MVC framework.

Another thing I am not sure about is if I need to populate a drop down in the view with data from the database I am running the EntityLoad() right in the view to get the data for the drop down. Again, this is technically going around the model to communicate with the database via ORM.

I guess I am just confused about the best practices of using ORM in a strict MVC framework. Should I have separate methods in my model files for every interaction with the database and then just have the view or service layer communicate with those methods in the model? The organization of everything seems to be lost when I try to think about it like that.

Just trying to wrap my mind around this.

Any help on this subject would be greatly appreciated.

Thanks in advance.

like image 965
Sequenzia Avatar asked Apr 09 '26 21:04

Sequenzia


1 Answers

In my opinion, using ORM directly from the service layer is fine. There is probably not a good reason to abstract further. Think of the ORM functions as a Gateway or DAO. You could put those functions into a Gateway or DAO if you really wanted to, some do.

Using ORM directly from t he view, however, is wrong. Don't do it. There are a couple better ways to handle it.

  • Have the data you need returned with the other data to build the page from the controller (which talks to the model).

  • Make a remote call from the view to the service layer (Ajaxy) to get the data.

There are probably (possibly?) other ways to handle it. But the bottom line is that your view should not be talking to the model layer.

like image 133
Jason Dean Avatar answered Apr 11 '26 22:04

Jason Dean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!