Jump to content

mvc using factory to return models where to call the model validation?


garbagedigger
Go to solution Solved by garbagedigger,

Recommended Posts

So I am working on an MVC application where the controller methods instantiate a factory that is suppose to return a model. In this case, the factory methods do some kind of work with a database or API to save/get/ data which then is placed inside a model and returned by the method.


So lets say I have a factory method called createUser(email, password); Now I am going to call this method in the controller. I want my controller lean and mean and not fat! The model can be fat but NOT the controller and so my methods for validation at the moment are inside the model. Where do I call this validation? Inside the controller before the factory? Inside the createUser() method in the factory before it does it's database and api work to return a model???


Where?!?


Here are two scenarios that come to mind:


-Instantiate the User model inside the controller method before the factory and validate the input data. If it returns without any problems, send it to the factory to return a model of the User with the newly created data.


-Instantiate the factory, call createUser(input data) and inside the create user method, do the model validation first before getting/saving the data to an API or database? If the validation fails in the createUser() method, return a model with error messages and any of the bad data. If their are no validation problems, return a model.

Link to comment
Share on other sites

Validation is context sensitive. Simply having an isValid method does not tell what it is valid for. Read http://martinfowler.com/bliki/ContextualValidation.html

 

You can opt to name your validation methods: validateForPersist instead of isValidForPersist in order to return an array which would be awkward in the latter.

Link to comment
Share on other sites

  • Solution

Interesting.  So from what I gather, it's better to build validation for solving specific situations right? If that is the case, is it almost better to build separate validator classes which represent specific situations and decouple the validation methods from the model?

 

I still don't understand if it's bad practice to do the validation inside the factory methods like factory->getUsers(input data) or factory->updateUsers(input data) or where in the process the validation should really start.

 

I was jut reading one of Martin's articles on mappers.  http://martinfowler.com/eaaCatalog/dataMapper.html.  He definitely has some good things to say about application architecture. It's a great article for describing ways of decoupling the database from the model.

Edited by garbagedigger
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.