Zend框架模型设计
我正在 Zend Framework 中构建一个应用程序。我的模型由三层组成:域类、数据映射器类和为所有外部通信提供接口的服务层。目前,数据映射器类仅在我的服务层中使用,域类是简单的 php 对象,仅包含特定于其域的信息。我的意思的一个非常粗略的例子......
//domain class
public class User{
// data and functions specific to the user domain
}
//service class
public class UserService{
// service contains an instance of the mapper class
protected $_mapper =
public function fetch($id){
return $this->_mapper->find($id);
}
}
//mapper class
public class UserMapper{
protected $_dbTable = new Zend_Db_Table('user');
public function find(){
return new User($this->_dbTable->find($id)->current());
}
}
我会像这样在控制器中检索一个用户对象
$user = $this->_service->fetch($id);
到目前为止这一切都很好,但现在我想这样做
$user = $this->_service->fetch($id);
$recipeCount = $user->getRecipeCount();
新数据来自另一个表,我不想每次加载用户时从数据库中提取所有这些信息,我想在调用 getRecipeCount 函数时延迟加载该信息。我想我的问题是实现这一目标的最佳实践是什么?就消费者而言,该信息特定于用户的域,因此我认为应该从 User 类调用它。为了实现这一点,域类需要包含自己的映射器实例,但这是否否定了首先拥有服务类的意义?我真的不想每次都这样做。
$recipeCount = $this->_service->getRecipeCount($user);
任何想法将不胜感激
I am building an application in Zend Framework. My model is made up of three layers, domain classes, data mapper classes and a service layer which provides an interface for all external communication. Currently the data mapper classes are only used within my service layer and the domain classes are simple php objects only containing information specific to their domain. A very rough example of what I mean....
//domain class
public class User{
// data and functions specific to the user domain
}
//service class
public class UserService{
// service contains an instance of the mapper class
protected $_mapper =
public function fetch($id){
return $this->_mapper->find($id);
}
}
//mapper class
public class UserMapper{
protected $_dbTable = new Zend_Db_Table('user');
public function find(){
return new User($this->_dbTable->find($id)->current());
}
}
I would retrieve a user object in the controller like so
$user = $this->_service->fetch($id);
This has worked fine so far but now I want to do this
$user = $this->_service->fetch($id);
$recipeCount = $user->getRecipeCount();
The new data comes from another table and I don't want to pull all this information from the database every time I load a user, I want to lazy load that information when the getRecipeCount function is called. I guess my question is what would be best practice to achieve this? As far as the consumer is concerned the information is specific to the domain of the user so I think that it should be called from the User class. To achieve this the domain class would need to contain its own mapper instance but is that negating the point of having the service class in the first place? I dont really want to be doing this everytime.
$recipeCount = $this->_service->getRecipeCount($user);
Any thoughts will be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定一个 User 对象,毫无疑问,您可以询问有关该用户的大量信息:获取他的所有组、获取他的所有帖子、获取他的所有食谱等。
很容易认为其中每一个都必须代表一种方法User 对象本身:
在这种情况下,像 Doctrine (#FTW!) 这样的 ORM 可以帮助管理这些类型的关系,为您提供一种查询语言,您可以使用它贪婪加载或延迟加载用户实体图。
但我认为这里有不同观点的空间。为什么这些必须是 User 对象本身的方法?它们是否可以是服务/存储库类上的方法:
这些服务/存储库类中的每一个都可能使用其自己的映射器类来实例化,以访问其底层数据源。这样,在创建/加载用户对象时就不存在惰性/贪婪的问题。如果您想获取相关实体,请在您需要时通过适当的服务/存储库直接访问它们。
并不是说它应该总是这样或那样。事实上,恰恰相反:两种方法都有自己的位置。选择在哪种情况下使用哪一种既涉及对性能的客观分析,也可能涉及不少个人审美。
只是大声思考。 YMMV。
Given a User object, there is no doubt tons of stuff that you could ask about this user: get all his groups, get all his posts, get all his recipes, etc.
It's tempting to think that each of these must represent a method of the User object itself:
In this case, an ORM like Doctrine (#FTW!) can help to manage these types of relationships, provide you with a query language with which you can greedy load or lazy load the user entity graph.
But I think that there is space for a different view here. Why do these have to be methods on the User object itself? Could they instead be methods on service/repository classes:
Each of these service/repository classes would probably be instantiated with its own mapper classes for accessing their underlying data sources. This way there is no issue of lazy/greedy on creating/loading the User object. If you want to get the related entities, then go ahead access them via the appropriate service/repository, precisely when you want them.
Not saying it should always be one way or the other. In fact, saying precisely the opposite: that both approaches have their place. Choosing which one to use in which context involves both some objective analysis of performance and probably no small amount of personal aesthetics.
Just thinking out loud. YMMV.
这似乎是一个在 Model_DbTable_Users() 中定义表关系会派上用场的地方。这样,您就可以在您认为合适的级别上对用户执行
findDependentRowset()
查询。尽管看起来您可能希望将其置于服务级别。表关系
This seems like a place where having table relationships defined in
Model_DbTable_Users()
would come i handy. That way you could just perform afindDependentRowset()
query on Users at whichever level you felt appropriate. Although it looks like you would likely want to put it at the service level.Table Relationships