Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Repository from a Repository

I have a two repositories Catalog and User, I have a situation where I need to call a method within the catalog repo from the user repo, is this good practice or is there a better way?

like image 983
monkeylee Avatar asked Jul 27 '09 11:07

monkeylee


2 Answers

You shouldn't be handling those kind of authorization checks within your Repositories. A business rule like "This user requires X comments to post" isn't really a repository query, it's a property of your User.

Also, authorization calls are made very frequently in an application, and you really don't want to hit your database every time a check is required.

You should properly load these permissions into your User object which is then cached for the current request, and use your domain:

public class Service {

    public void Save(Post post)
    {
        if(User.GetCurrentUser().HasEnoughCommentsToPost())
            postRepository.Add(post);
    }

}
like image 77
Mike Gardiner Avatar answered Sep 28 '22 05:09

Mike Gardiner


I would reference the other Repository at the upper layer, such as a service layer

like image 38
dfa Avatar answered Sep 28 '22 03:09

dfa