8

This question is about applying rules of my application that confuse me.

My controller is using service and the service is using repository.

public class CommentController: ApiController{

    [HttpPost]
    public bool EditComment(Comment comment){
        commentService.Update(comment);
    }
}

public class CommentService{
    ICommentRepository repository;
    ....
    ....
    public void Update(Comment comment){
        repository.Update(comment);
    }
}

If the user is authenticated, he can update a comment.

  • But a user should edit own comments.

  • But an admin can edit all comments.

  • But comment can not edit after a specified date.

  • Edit by a department

And I have something like these rules.

If I apply "user editing own comment" rule in service layer, I will change Update methot and pass parameter of controller User.Identity.Name,

public class CommentService{
    ICommentRepository repository;
    ....
    ....
    public void Update(string updatedByThisUser, Comment comment){
        // if updatedByThisUser is owner of comment
        repository.Update(comment);
    }
}

But, is true changing service operations by rule?

I am confused a bit about where can I apply the rules. In controller or in service or in repository.

Is there any standart way to do this like design patterns.

  • In my experience, it's always good to apply authorization at the Controller level. You would place an authorize attribute above the Controller class name like so: [Authorize(Roles="member, admin")] and now all of the action methods in the Controller would only be accessible by users in the role of "member" or "admin". – Alternatex Jan 27 '16 at 10:07
  • 1
    But, If I need to use my service on desktop project and MVC web site project, I should apply Authorize rules all projects again. – barteloma Jan 27 '16 at 11:30
1

I would

  • create a seperate PermissionService that implements methods like isUserAllowedTo(user, PermissionService.Permissiontype.Update, PermissionService.Topic.COMMENT, additionalContextRelevantParameters)
  • and call the PermissionService methods in the controller, where neccessary context info (i.e. from session) is available.

You can design your architecture to call the permission service in the CommentService. But i would not recommend this because this would add additional dependencies into the service (i.e. session) that makes unit-testing the service much more difficuilt

1

First, take some time to consider what the comment service is actually responsible for. This will depend on your specific requirements, and possibly even your own best judgment.

If the comment service's responsibility is limited to updating any given comment, then what you have written is well. In this case, you will need to consider some other conditional logic for checking whether any given comment can be updated. You will probably do this in the controller, or you may create a separate sub system for checking if a User can post a comment. You could re-use this just like you plan on re-using the comment service.

If the comment service is responsible for updating a user's comments, then your service becomes Update(User user, Comment comment) and you have flexibility to check business rules within the service itself.

Now that you are clear on what your design intentions are, you will have clarity on how to implement your solution.

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.