joh*_*ack 7 php architecture separation-of-concerns
我希望能够改变和传递我的UserEntity的某些部分,并且某些部分应该保持不变.
例如,我从不想改变我的UserEntity的id,但是电子邮件或密码之类的东西可能会经常更改,并且也可以被UserEntity之外的其他对象使用.
其中一个例子是创建UserEntity时.由于UserEntity在没有id的情况下不能存在,我的控制器可以创建一个UserData对象来标准化UserEntity属性.在映射器在db中创建实体之后,它将创建一个新的UserEntity并传入构造函数中的id和UserData对象.
当UserEntity需要电子邮件或密码等信息时,它只能查看其UserData.
看起来更便携,但这有点过分吗?有更好的解决方案吗?
注意
我认为这可能是好的原因:可变字段的值需要标准化......有时这些字段需要在实体本身之外传递.例如,在创建实体之前.通过创建一个可以传递的值对象,我们提供了一个标准化的点,可以从任何地方分配这些值,以及可以在实体外部传递的东西.
通过"标准化",我的意思是我的信息需要统一,无论它存在于何处.例如,email
需要总是n
长度和有效的格式,name
总是需要n
长度等.我的目标是,我希望能够在一个地方设置这些"规则"...并且因为这些UserEntity(可变的)的属性存在于实体本身之外,有时,它们可能在自己的值对象中独立存在.
我不认为有“一种真正的方法”可以做到这一点(无论你读到什么)......如果它在你的模型中有意义,那么它对我来说听起来不错。当您说“其中许多字段需要标准化”时,我不确定您到底是什么意思,以及为什么不能将其作为 UserEntity 的一部分来完成,但无论如何。也就是说,即使没有完全独立的对象类,您也有可能准确地完成您想要做的事情。
评论/批评:
您所建议的内容并不真正符合严格的“对象”模型,即 UserData 只是由 UserEntity 的真正属性组成,并且与这些属性没有其他潜在关系。
我不太确定为什么您需要一个单独的对象在实体外部传递...如果您需要数据,为什么不能只传递 UserEntity 并从那里访问它?在将数据传递给 UserEntity 构造函数之前,您需要对数据执行什么操作,这不能通过在 stdClass 实例中将数据收集在一起然后在 UserEntity 中处理它来轻松完成?
如果是我,我会做类似以下的事情(例如,创建一个新用户):
<?
// assume an appropriately defined UserEntity class...
// I'm using stdClass just to keep the parameters together to pass all at once
// I'm assuming some basic user data passed from the browser
$user_data = (object) array(
'email' => $_REQUEST['email'],
'name' => $_REQUEST['name'],
'password' => $_REQUEST['password'],
'confirm_password' => $_REQUEST['confirm_password']
);
/*
validateData is static so it can be called before you create the new user
It takes the $user_data object to validate and, if necessary, modify fields.
It also takes a $create flag which indicates whether the data should be
checked to make sure all of the necessary fields are there to create the user
with. This allows you to call it on update with the $create flag unset and it
will pass validation even if it's missing otherwise required fields.
It returns $result, which indicates pass or failure, and the potentially modified
$user_data object
*/
$create = TRUE;
list($result, $user_data) = UserEntity::validateData($user_data, $create);
// equivalence allows you to pass back descriptive error messages
if ($result === TRUE) {
// create the user in the database, get back $user_id...
$user = new UserEntity($user_id, $user_data);
}
else {
// return error to user
}
// access user data either individually, or if you want just make a getter
// for the entire group of data, so you can use it just like you would a
// separate UserData object
send_double_opt_in($user->getUserData());
?>
Run Code Online (Sandbox Code Playgroud)
编辑以解决提供的更多信息:
您说这些属性存在于 UserEntity 之外,并且它们可能独立存在...您的意思是这些属性可以被收集、使用和丢弃,甚至不需要用于 UserEntity 对象?如果是这种情况,那么一个单独的对象将完全适合该数据。如果不是,如果数据始终从属于现有或未来的 UserEntity,那么从……让我们称之为“全局数据”的角度来看,这些属性将永远不会“独立存在”。当您将整个系统视为一个整体,而不仅仅是时时刻刻的代码时,数据很可能“属于”UserEntity 类。
至于静态方法,我认为没有特别的理由来避免它们(显然),但每个人都有自己的理由。许多其他架构会稍微复杂一些,但这里有一些选项:
$this->status = 'error';
或类似的东西来告诉您发生了需要处理的不良情况)。$status
属性来指示验证失败。 归档时间: |
|
查看次数: |
251 次 |
最近记录: |