views:

58

answers:

2

Hello,

I was wondering if i can use the Repository Pattern for anything other than a database layer?

I have used the pattern for a shopcart where I stored shopcart items in an array in ShopCartRepository class. I then got the data from the ShopCartRepository using a ShopCartController class.

As mentioned the ShopCartRepository only deals with an array and therefore not a database.

Is this a "legal" approach for using the Repository Pattern?

A: 

The Repository Pattern is not only for the Database.

It is typically, however, used to abstract away some sort of method for working with domain objects from a data layer. If you're using it strictly for working with a ShoppingCart, why not add the methods to the ShoppingCart so the cart can behave like a cart normally would rather than having to worry about working with the ShoppingCard AND a ShoppingCartRepository?

Justin Niessner
I agree, the only time I have seen a repository used is if you are selecting multiples of the item. In the case of the shopping cart you would never need to show your user more than one cart so it seems these methods should be handled in the object itself.
Lumpy
+3  A: 

The repository pattern is good for any situation in which you want to have a centralized place to control the creation, retrieving, modification and deletion of data proceding from a set, be it a database table, an in-memory list, or whatever; this power of abstraction is precisely what makes the pattern valuable. Of course it is legal to use it in any way that makes sense for your project.

Konamiman