php模型映射接口,php – 实现3结构模型(域对象,数据映射器和...

正如标题所示,我在实现3结构模型(域对象,数据映射器和服务)时遇到了一些小问题.

过去,当有人在我的网站上注册时,我会这样做

$user->register($firstName, $lastName, $emailAddress, $username...);

并且该方法将按照这样的步骤运行

1. Check if the form sent was valid.

2. Check if all the required fields were filled.

3. Check the if the lengths of strings were valid and the range of integers etc.

4. Check if the input is in the correct format (regex).

5. Check if the username is already taken and if the email address already exists

in the database

6. etc. etc.

一切正常,但我正试图摆脱这样做,因为我希望我的代码更可重用和可测试.

现在,通过这个3结构模型,域对象和数据映射器应该通过服务进行通信,以使它们彼此隔离,所以这是我对用户服务的想法

class UserService {

public function register($firstName, $lastName, $email...) {

$userDO= $this->domainObjectFactory->build('User');

$mapper = $this->dataMapperFactory->build('User');

// Is this where I start doing my validation like in the steps above???

// And if this is where I start doing my checks, when I get to the part

// where I have to check if the username they want is already taken how

// how do I do that check?

}

}

然后实际运行我会从我的控制器这样做

$userService = $this->serviceFactory->get('user');

$result = $userService->register($_POST['firstName']....);

逻辑(if和else)必须放在我的UserService类的register()方法中吗?因为如果他们进入域对象,当我到达需要数据库进行某些检查的阶段时,如果用户名已经存在,我将如何访问数据库?我真的不知道,因为域对象不应该知道有关数据源的任何信息.

必须有一种方法来访问数据库以进行小型查询,例如检查用户名或电子邮件地址是否已经存在以及是否需要执行其他一些小查询.

我有很多实体/域对象需要做大量的小查询,而且在过去我的模型可以从任何方法中访问数据库并可以执行这些查询但这似乎不允许使用这个3结构模型我很想知道这样做的正确方法是什么,因为必须要有办法.

我飞行它直到我发现一个模型是一个分为3个结构的层.

任何帮助或推动正确的方向将非常感激,尤其是现实生活中的好例子.互联网似乎缺乏我的具体问题.

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值