【问题标题】:Interface and Repository naming in PHP and LaravelPHP 和 Laravel 中的接口和存储库命名
【发布时间】:2014-09-13 10:43:39
【问题描述】:

我正在使用 Laravel 中的存储库模式和接口,发现命名很冗长:UserRepositoryInterface 或 EventRepositoryInterface。是否还有其他不那么冗长的命名约定?我希望我的代码对未来的程序员保持可读性。在 DDD 中,有些人建议将 UserRepositoryInterface 简单命名为 Users 或 AllUsers,更多信息请参见:How to name repository and service interfaces?

【问题讨论】:

    标签: php laravel interface repository naming


    【解决方案1】:

    命名界面的“好”方式应该是 UserRepository。

    当使用一个接口实现一个类时,你正在制定一个契约:这个类将使用一个 UserRepository。不是一个接口,而是一个实际的存储库。

    您的类将依赖于接口的实现,但不关心在连接所有组件时会给出哪个实现。

    你应该为你的实现命名以反映它们的特殊性:例如,基于 Eloquent 的存储库的 EloquentUserRepository。

    拥有 DefaultUserRepository 毫无意义。您的任何实现都不应该比其他实现更好,并且应该得到特殊处理。

    我在括号中写了“好”,因为最后这更像是您团队的惯例问题,您应该做适合自己的事情。

    您可以在这篇有趣的博客文章中阅读更多关于此主题的信息:http://verraes.net/2013/09/sensible-interfaces/

    【讨论】:

      【解决方案2】:

      我建议您将接口命名为 UserRepository 和实现,例如 EloquentUserRepositoryMongoUserRepository 等等。当大多数 IDE 能够在闪存中找到正确的文件时,现在不需要在接口名称中保留 interface

      【讨论】:

      • UserRepo 和 EloquentUserRepo 呢?这会被认为是“标准的”和可读的吗?
      猜你喜欢
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多