Recently I got a chance to contribute to a project. The project is very vast and contains many independent modules like brand section, store section, user section etc. All these modules contain respective models that contains business logic. Admin has all capabilities like adding a brand to a parent organisation. Creating a user and assigning him any roles.
There are many business logic in built into each module. For example, normal users can not add any other user, but users with manager role can create 5 users under them. Admin can create any number of users. Similarly there are many other constraints.
Right now what I am seeing is all validation has been put into controller layer. For example the controller first checks that if the user is normal user or admin or manager. Then if a manager, it checks for how many users he has already etc etc.Same goes with brands module and stores module. Right now admin module has no model, it just uses models from other module to perform operations. This has made the controller very thick. Also I think that these validations should go into model as these are business rules.
But if I put these validations into model of respective modules, every function needs to be in two flavours, one for admin and another for non admin. These will lead to duplication of code.
- Is the current design of admin controller using other models is fine and maintainable.
- Is it ok for models to have only generic functions, like right now user model is having generic functions like add user, check number of delegated users for a user and controller is using both of these methods to perform the operation.
- Should I write a wrapper over admin service wrapper for each module ? But this will destroy the cohesiveness of admin module.
Right now the product is working fine soon a number of other modules will be added. How can I make sure that code is maintainable ? Also I have read at many places that a model should talk to another model using services only. Should I write an admin service for each module ?