I recently read a blog post from Adam Wathan: Active Repository is an anti-pattern and I have to say I don’t entirely agree.

I use Active Record pretty much exclusively. I don’t work on large apps or anything I’ve seen a huge value in switching to Data Mapper for.

I also use repositories, and don’t think it is an “anti-pattern” and here is why:

Adam’s biggest complaint seems to be that people are trying to manage a model’s relationships through a repository. To me this was never the purpose of a repository. Shouldn’t a repository be where you store something?

repository definition

When you start using a repository as not a repository, then you are doing something wrong. You can get stuff, and look for stuff, but your tool shed shouldn’t be replacing the blades on your table saw for you.

You can use repositories with Active Record without getting crazy trying to manage individual models. That’s not what a repository is for.

Take these simplified examples of using a Repository and Active Record:

$posts = new PostRepository;

How about getting something from a repository and adding a relationship?

$title = 'My old post';
$comment = 'Dude, sweet.';

$post = $posts->getByTitle($title);
$post->addComment(new Comment($comment));

Or anything else… Use the repository for what it is intended, and use the strengths of Active Record for managing the Model and its relationships. I agree with Adam’s complaints but not the title of his post.

What are your thoughts?