This is a design pattern question for MVC models.
I'm using GeddyJs (for Node.js), but it's the same pattern as Sails.js and so on.
Sometimes I get to a point, where I don't know if I should place the code into the Controller, or into the Model.
For example:
I have a SerialKey
model, where each serial should have a unique generated id, accordingly to my algorithm (let's say, a random name like blue cat
, nice car
...).
Is this process of generating, checking if it exists in the database, and inserting, something that should be done by a Controller, or a Model?
Because as I see, every create method should follow this pattern, meaning that it should be on the Model. However, since it's logic stuff, should be in the Controller...
What to do in those situations? What ever place I put it, if it works it's ok?
Is this process of generating, checking if it exists in the database, and inserting, something that should be done by a Controller, or a Model?
Model. I prefer the "fat models, skinny controllers" design, although of course there are varying opinions here. I view controllers as "glue code" and they are harder to test since they have collaborators on both the input and output sides. Thus keep them skinny. Generating default values, ensuring constraints such as uniqueness and other data-oriented logic goes in models.