About the Repository pattern in the MVC architecture

About the Repository pattern in the MVC architecture

http://www.cnblogs.com/dudu/archive/2011/05/25/repository_pattern.html

	个人理解:Repository模式是一个单独的分层,他介于service层和dao层之间,他是采用接口的方式来访问dao层的,并且Repository模式会让servic层在访问dao层时,dao层是不知道的,Repository充当了一个仓库管理员,service层(领域)只需要告诉Repository自己需要什么就可以了,Repository会把东西拿给他


Tabbycat's understanding (source):

  1. Repository mode is an architectural mode, and it has reference value when designing the architecture;
  2. Repository mode is mainly to encapsulate data query and storage logic;
  3. Practical purpose of Repository mode: replace and upgrade ORM engine without affecting business logic;
  4. Repository mode can improve test efficiency. In unit testing, using Mock objects instead of actual database access can double the speed of test cases.
    Evaluation: The benefits of applying the Repository pattern are far higher than the code added to implement this pattern. This mode should be used as long as the project is layered.
    Regarding the generic Repository interface (source): It
    is not appropriate to only use the generic Repository interface, because the Repository interface is an operation contract provided to the Domain layer, and different entities may have different operation constraints for the Domain. Therefore, the Repository interface should still be defined separately for each Eneity class.
    The generic Repository class is still used to reduce duplication of code, but it cannot be directly inherited by the UserRepository class, because the Delete method will invade the User class, so it is changed to combine a Repository in the UserRepository, which will be open to the domain and can use generics. The function of reuse is entrusted to this Repository
    . The difference between Repository and Dal (source):
    Repository is a concept in DDD (Domain Driven), emphasizing that Repository is Domain-driven. The functions defined in Repository must reflect the intentions and constraints of Domain, while Dal More purely is to provide data access functions, not strictly limited to the Business layer.
    The use of Repository implies a tendency of intention, that is, I will provide what the domain needs, and do not provide functions that should not be provided. Everything is based on the needs of the domain; while using Dal, the intention tendency lies in my Dal layer The database access operations that can be used are provided to the Business layer, and you can choose which one you want to use. You can also use my Dal for another business. Everything is based on what operations my Dal can provide.

In order to build MVC applications that are more adaptable to future changes and easier to test, you should consider using the Repository pattern. When you use the Repository pattern, you will create a separate repository class, which contains all the data access logic.
When you create the repository class, you create an interface that represents all the methods used by the repository class. In your controller, you write code for the interface, not for the repository. In this way, you can use different data access technologies to implement the repository in the future.

Part of the explanation refers to the celebrity of CSDN, the source can not be found, the infringement is deleted!

Guess you like

Origin blog.csdn.net/DoChengAoHan/article/details/102660630