1

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.

  1. Is the current design of admin controller using other models is fine and maintainable.
  2. 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.
  3. 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 ?

  • Why is admin a module in your system, when all described requirements indicate that admin should actually be a role, similar to "user" and "manager"? – Bart van Ingen Schenau Oct 29 '17 at 14:54
  • I guess this is to protect all admin routes by one middleware. Middleware checks the token to ensure that the user has admin permission. Then admin controller uses other modules to serve. – Krrish Raj Oct 29 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.