What specific issue does the repository pattern solve?

后端 未结 7 1142
自闭症患者
自闭症患者 2020-12-12 12:55

(Note: My question has very similar concerns as the person who asked this question three months ago, but it was never answered.)

I recently started working

7条回答
  •  孤街浪徒
    2020-12-12 13:28

    Entity Framework's DbContext basically resembles a Repository (and a Unit of Work as well). You don't necessarily have to abstract it away in simple scenarios.

    The main advantage of the repository is that your domain can be ignorant and independent of the persistence mechanism. In a layer based architecture, the dependencies point from the UI layer down through the domain (or usually called business logic layer) to the data access layer. This means the UI depends on the BLL, which itself depends on the DAL.

    In a more modern architecture (as propagated by domain-driven design and other object-oriented approaches) the domain should have no outward-pointing dependencies. This means the UI, the persistence mechanism and everything else should depend on the domain, and not the other way around.

    A repository will then be represented through its interface inside the domain but have its concrete implementation outside the domain, in the persistence module. This way the domain depends only on the abstract interface, not the concrete implementation.

    That basically is object-orientation versus procedural programming on an architectural level.

    See also the Ports and Adapters a.k.a. Hexagonal Architecture.

    Another advantage of the repository is that you can create similar access mechanisms to various data sources. Not only to databases but to cloud-based stores, external APIs, third-party applications, etc.

提交回复
热议问题