Setting up a repository pattern in MVC

前端 未结 2 1069
悲&欢浪女
悲&欢浪女 2021-02-20 10:37

I\'m trying to figure out how the Repository pattern works and how it can be implemented in a custom MVC pattern.

As far as i understand it, the Repository is a layer wh

相关标签:
2条回答
  • 2021-02-20 11:03

    Yes, this is a correct implementation of the Repository pattern. The DAO pattern is often useful as well, but there's nothing wrong with your implementation.

    DAO is simple a pattern that separates your persistence logic from your business logic. It would create CRUD operations while your entity would contain methods for your business logic, so it's separating the responsibilites of persistance from your domain. I usually go for DAO for single entities and repositories for aggregates, allowing me to do things like productCatalogRepository.Update(), which in turn will iterate over the product DAOs and have them store themselves.

    0 讨论(0)
  • 2021-02-20 11:08

    Your Repository looks much more like a TableDataGateway to me. The idea of a Repository is to be another layer on top of the mapping layer that mediates between the domain objects and the database. It also serves as an in-memory storage of domain objects (something that is missing from your example) and may encapsulate a Factory for creating new Entities. They often also allow for querying the Repository by Specification patterns:

    Repository Sequence Diagram from POEAA

    It's a rather complex pattern. You can find good write-ups about Repository in

    • Fowler: Patterns of Enterprise Application Architecture
    • Evans: Domain Driven Design
    • http://thinkffffd.com/assets/2/Domain_Driven_Design_-_Step_by_Step.pdf

    Also check Good Domain Driven Design samples

    0 讨论(0)
提交回复
热议问题