jmagaro88's avatar

Repositories within Repositories?

I'm working on an eCommerce application and using the repository pattern. I am using the database to store Carts and Cart Items so that customers can come back to their Cart after their session might have expired. Each Cart Item belongs to a Cart and has a Product Option, quantity, etc. And each Product Option belongs to a Product and has its own description, price, etc.

My issue arises on the cart page. I have a Cart Item repository that can retrieve all the Cart Items for a given Cart id from the database. However, each Cart Item has multiple other dependencies like Product and Product Option. For example, if a customer adds an item to his cart with Product Option 1 and Quantity 1, I can't simply display that information to the customer. I need to retrieve the description and price for Product Option 1 from the Product Options table, as well as the description for Product Option 1 from the Products table, and display them to the customer on the cart page.

To keep with the repository pattern, I want my Cart Item repository to use the Product and Product Option repository to retrieve the appropriate information. However, this introduces a problem where I am needing to inject the Product and Product Option repositories into the Cart Item repository. I read somewhere that it is bad practice to inject foreign repositories into another repository.

As a result, I have created a Cart Item service where I inject all of the necessary repositories. However, I feel that this is kind of a cluttered setup, and I would like to stay away from using catch-all service classes as much as possible. Is there a better organization of this that anyone could suggest?

0 likes
3 replies
luddinus's avatar

Services are fine, repositories are overrated.

jmagaro88's avatar

I mean, ultimately the service class works. I just am probably over-concerned with having a really tight application design. Why do you say repositories are overrated?

jlrdw's avatar

The same issue has come up on repositories vs putting the code in controller. Folks you have to remember one thing, it doesn't make a hill of beans where the code is located, the CPU does not care when it processes it, it processes one line at a time. So really it doesn't hurt having the code for things inside of a controller.
When it comes to that CPU processing there is no such thing as a OOP, all code is processed procedurally.

Please or to participate in this conversation.