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.
[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".