Introdution

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend on details. Instead, details should depend on abstractions.

Domain Modeling

Repository Pattern

The Repository pattern is an abstraction over persistent storage

  +-----------------------------+
  |      Application Layer      |
  +-----------------------------+
                 |^
                 ||          /------------------\
                 ||----------|   Domain Model   |
                 ||          |      Objects     |
                 ||          \------------------/
                 V|
  +------------------------------+
  |          Repository          |
  +------------------------------+
                 |
                 V
  +------------------------------+
  |        Database Layer        |
  +------------------------------+

主要内容:如何使用依赖反转将领域模型和数据库解耦

  • 传统Layered architecture和Onion architecture的差别
  • 如何实现Inverting the Dependency: ORM Depends on Model (以SQLAlchemy为例)
  • 如何测试domain -> repo
  • 如何测试repo -> database
  • 如何创建一个Fake repo进行测试
  • 使用Repository Pattern的tradeoff(Pros and cons)

拓展阅读

除另有声明外,本博客文章均采用 知识共享(Creative Commons) 署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 进行许可。