简体   繁体   English

从DAO组装域对象的逻辑应该放在哪里?

[英]Where should reside the logic to assemble the domain object from DAO?

I've read almost every questions here and a lot of related argument in the www, but still I'm not sure to well understand the point, and probably I miss something that is obvious to everybody else, since I think is quite a common situation... 我在这里阅读了几乎所有问题,并在www上阅读了许多相关的论据,但我仍然不确定是否要理解这一点,并且可能我错过了其他人显而易见的内容,因为我认为这很普遍情况...

Please, forgive my terrible english and the mixed up terminology, but I'm not really interested in differences/advantages/caveats of DAO vs Repository, I think this does not change the 'core' of the question, but maybe I'm wrong. 拜托,请原谅我糟糕的英语和混乱的术语,但是我对DAO与存储库的差异/优势/不足并不真正感兴趣,我认为这不会改变问题的“核心”,但也许我错了。

Obviously the example is too simple and every solution is easily overkilling, but think at this as a 'case' for a much bigger system. 显然,该示例太简单了,每个解决方案都容易过大,但请以这为更大系统的“案例”。

Assume you have to build an application to suggest salesmans the people to call. 假设您必须构建一个应用程序来建议推销员与他们联系。

Each prospect has some 'textual' data (ie name, gender, birthdate, address, phone number, email,...), a photo and some history of his/her interactions with other persons and vendors. 每位准客户都有一些“文本”数据(即姓名,性别,生日,地址,电话号码,电子邮件等),一张照片以及他/她与其他人和卖方互动的一些历史记录。

Textual data resides on a Mysql table (person), the photo somewhere in the filesystem and someone has already released services that return a list of persons potentialy interested to be contacted given the salesman and a score for every prospect as a buyer. 文本数据驻留在Mysql表(人)上,文件系统中某处的照片上,有人已经发布了服务,这些服务返回给销售员的潜在潜在联系人列表,并给出每个潜在买家的得分。

I'll probably end up with the following: 我可能会得到以下结果:

  1. A Domain Object Person, with name, phone number, address, email and photo. 域对象人,带有名称,电话号码,地址,电子邮件和照片。 Setters and getters for all the property plus a methods getScore(). 所有属性的设置器和获取器,以及方法getScore()。

  2. Two DAO, one for the mysql table and one for the filesystem. 两个DAO,一个用于mysql表,另一个用于文件系统。

  3. A service to get the list of the prospect for the given salesman (the user of the application, left out of the scope of this example). 用于获取给定业务员(应用程序的用户,不在此示例范围内)的潜在客户列表的服务。

What is not clear to me: 我不清楚的是:

  1. The method getScore() in the Domain Object could directly call the service in the service layer? 域对象中的getScore()方法可以直接在服务层中调用服务吗? If not, why? 如果没有,为什么?

  2. Do I need separate DTO for data coming from different DAOs? 对于来自不同DAO的数据,我是否需要单独的DTO?

  3. If so, I need some sort of manager or 'super' DAO that own the logics about how to assemble the two pieces of information (ie get the URI from the mysql DAO, retrieve the file, load the picture)? 如果是这样,我需要某种管理器或“超级” DAO,它们具有有关如何组合这两条信息的逻辑(即从mysql DAO获取URI,检索文件,加载图片)? Should this reside in the Data Layer or in the Service Layer? 它应该驻留在数据层还是服务层? (seems to me should stay in the data layer, as far as it deals with were data are stored). (在我看来,就应该存储数据而言,它应该保留在数据层中)。

  4. The output of the manager or 'super DAO' should be another DTO or could be directly the Domain Object? 管理器或“超级DAO”的输出应该是另一个DTO,还是可以直接是域对象?

  5. If DTO, do I need a service who call the manager/super DAO and build up the domain object (in this case I assume the service will 'add' the score to the domain object to). 如果是DTO,我是否需要一个服务来调用经理/超级DAO并建立域对象(在这种情况下,我认为该服务会将分数“添加”到域对象)?

Is an articulate question, I know, but I'm not able to figure out how to design the solution. 我知道这是一个明确的问题,但是我无法弄清楚如何设计解决方案。

A starting point for an answer: 答案的起点:

  1. I think the getScore() method should belong to a service not to a domain object 我认为getScore()方法应该属于服务而不属于域对象
  2. Matter of taste 味道的问题
  3. That's a service IMHO 这是服务恕我直言
  4. The output of the service could be a DTO or a domain object 服务的输出可以是DTO或域对象

In short (and in my opinion) 简而言之(我认为)

  • domain objects are "dumb" classes (just here to map datas with objects) 域对象是“哑”类(仅在此处将数据与对象映射)
  • DAOs are here to fetch datas (and that's it) DAO在这里获取数据(仅此而已)
  • DTOs can be used as mediators between DAOs and services DTO可用作DAO与服务之间的中介
  • services are here to perform pertinent actions (by using dao), in you case calculating the score for example 服务在此处执行相关操作(通过使用dao),例如在您计算得分的情况下

Usage is allowed from bottom to top, ie. 允许从下到上使用,即。 a service can use a DAO but not the other way around, to ease the possibility of changes (for example: DB backend change => DAOs updated and that's it) 服务可以使用DAO,但不能使用其他方法来减轻更改的可能性(例如:数据库后端更改=> DAO已更新,仅此而已)

=> community wiki =>社区Wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM