I have two models Permission and Role, now in roles/create, I need to show all the available permissions to allow admin to select the permission and create a role, now should I import both PermissionRepository and RoleRepository in RoleController and pass to roles.create view or I should retrieve all the permissions in RoleRepository. Please let me know, if I need to add anything else in this question to make it clear or if you want me to answer any question related to this question?


I would advise against importing both the Permission and Role repositories in your RoleController since that pretty much breaks the pattern you're trying to implement. There are certainly situations where you might consider deviating from a strict separation, but whenever performance is not absolutely critical it tends to add code complexity at no real benefit.

My suggest would be to retrieve the roles through the role controller and the permissions through a permission controller, and let the data aggregation be handled by the business logic layer, which it should be. Even in the case where very different sets of permissions are associated with different roles you should retrieve the roles seperately and then retrieve the permissions for those roles seperately.

  • Thank you for your interest in my question, I think I could not explain the situation correctly, let me explain you again. I have asked about retrieving the permissions in PermissionRepository or RoleRepository? Aug 29 '19 at 12:29
  • 1
    Your PermissionRepository should handle only the persistence of permissions. Your RoleRepository should handle only the persistence of roles. In your case that means that if your application requires some combined view over both then you should retrieve both from their respective repositories and combine the information in the business logic. In other words; you store the permissions in the permissionrepository and retrieve them through the permissioncontroller. You store the roles in the rolerepository and retrieve them through the rolecontroller. It's not a case of 'which' you should use.
    – DeBunkeD
    Aug 29 '19 at 12:34
  • Your explaination is good, but when I am using anything from any repository just like RoleRepository using method of PermissionRepository, I need to pass, Permission model instance as my PermisisonRepository constructor needs Permission model instance, so again I am using Permission model instance in RoleRepository, Can I avoid this? Aug 29 '19 at 12:48
  • For now, I am doing just like I have mentioned in previous comment, if I can improve it, please let me know, also, you should make your comment as your answer. Aug 30 '19 at 4:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.