SuperAPI AI 工具集

implement-repository-pattern

通用与其它

查看项目

更新于 10-16 05:42

技能介绍

用途:按 Clean Architecture 原则创建仓储,把领域层 Protocol(接口)与基础设施层实现分离,实现依赖倒置,用于新增数据访问层、创建数据库交互、存储或检索领域模型。

主要能力:在 domain/repositories/{name}_repository.py 用 ABC 与 @abstractmethod 定义 Protocol,所有操作返回 ServiceResult[T];在 infrastructure/neo4j/{name}_repository.py 实现 Neo4j 仓储,继承实现类与 ManagedResource,构造函数必传 driver 与 settings 并校验,数据库操作统一走 _execute_with_retry(),实现 close() 释放资源。Cypher 查询集中放在 infrastructure/neo4j/queries.py 的 CypherQueries 中,使用参数化查询与 MERGE 做 upsert。再在 container.py 以 {name}_repository() 注册,返回 Protocol 类型并注入依赖。最后编写单元测试(mock driver)与集成测试(真实 Neo4j)。

使用方式:按五步执行——建领域 Protocol、建基础设施实现、加 Cypher 查询、注册容器、写测试;配套模板与参考见 templates/protocol-template.py、templates/implementation-template.py、templates/test-template.py、references/pattern-guide.md、references/troubleshooting.md,脚本有 scripts/analyze_queries.py、scripts/generate_repository.py、scripts/validate_repository_patterns.py。

适用场景:需要为领域模型建立持久化、实现查询与命令、抽象数据访问时使用。

限制与要求:依赖 neo4j>=5.0.0 异步驱动,使用 project_watch_mcp 的 ServiceResult、ManagedResource 与 Settings;方法必须异步,错误用 ServiceResult.fail,不可直接使用 driver;完成前需通过单元与集成测试及质量门禁。资料未提供性能指标与扩展说明。