原则 2 中的代理、存储库和服务之间有什么区别?
我想知道这 3 种模式有什么区别。
据我了解:
- 代理仅用于延迟加载实体
- 存储库用于向模型添加逻辑(为 DQL 语句提供一些快捷方法)
- 服务用于依赖模型和控制器
对于最后一个服务,我不'它背后的 Doctrine 2 项目作为存储库并不知道背后的持久系统,因为它使用带有 DBAL 的 ORM。
我读到代理可用于向实体添加逻辑,那么与存储库有什么区别?
顺便说一句,我的实体是否应该仅包含其属性的公共 getter/setter?正确吗?
我有点迷失在这里。
你有具体的例子吗?
I am wondering what are the difference between those 3 patterns.
As far as I understand:
- Proxies are used only for lazy-load entities
- Repositories are used to add logic to your model (width some shortcut method for DQL statement)
- Services are used to rely models and controllers
For this last, services, I don't the point behind it with a Doctrine 2 project as repository are not aware of the persistent system behind, because it use the ORM with DBAL.
I read that proxies could be used to add logic to entities, so what's the difference with repositories?
By the way, should my Entities only contain public getter/setter to their attribute and only that? Is it correct?
I'm kind of lost here.
Do you have any concrete example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代理是对某事物的引用。
假设您正在创建一个用户,并且一个用户附加一个组。
如果您没有代理支持,则需要为组获取数据库才能使用实体。这是一个可能的代码:
有了对代理的支持,您不需要进行数据库查找。代理被认为是对数据库事物的引用,而不是实际获取它。您的代码:
拥有代理的好处很简单...如果您需要获取(例如)组名称,只需正常执行即可: $group->getName();代理将初始化实体(按需获取数据库)。
存储库是一个数据操作的东西。您用它来管理您的实体。因此,您可以 findById、findOneByEmail 等。您还可以扩展其基本功能并实现您自己的方法来管理您的实体,例如:saveUser、retrieveMostActiveUsers。
服务在内部使用存储库,但它包含更多验证,并且可能与 > 交互。 1 个存储库。例如,成功保存用户后,您想向他发送一封祝贺电子邮件。因此,在 UserService 的 createUser 方法中,您检索NotificationService 并发送新的注册电子邮件。
现在你清楚了吗? =)
干杯,
A Proxy is a reference to something.
Suppose you are creating a User and one User has one Group attached.
If you don't have Proxy support, you would need to do fetch on DB for the Group just to be able to use the Entity. Here is a possible code:
With support to Proxies, you don't need to do the DB lookup. A Proxy is considered a reference to a DB thing, without actually fetching it. Your code:
The benefit of having Proxy is simple... if you need to get (for example) the Group name, just do it normally: $group->getName(); And Proxy will initialize the Entity (DB fetch on-demand).
A Repository is a data manipulation thing. You use it to manage your Entities. So you can findById, findOneByEmail, etc. You can also extend its basic functionality and implement your own methods that manage your Entities, like: saveUser, retrieveMostActiveUsers.
A Service uses Repositories internally, but it contains more validation and may interact with > 1 Repository. For example, after you successfully save the user, you would like to send him a Congrat email. So inside your createUser method of UserService, you retrieve the NotificationService and dispatch the new registration email.
Is it clear for you now? =)
Cheers,