开发者

Repository Pattern: Is it only for database source?

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 Shop开发者_如何学编程CartRepository 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?


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜