老妖的博客
现实的中没有几个人能够真为对方去死,甚至山盟海誓很快就会在金钱面前变的微不足道,这才是生活。没有永远的爱,除了你的父母对你,当然也就没有永远的恨,更没有永远的痛,时间是最好的治疗大师,它会很快抚平你心灵上累累的伤痕。很多年以后你想起来时,那些在你生命中汹涌来往的人群至多是个模糊的影子或者毫无意义的名字
posts - 105,  comments - 171,  trackbacks - 0

在entity Domain Object 中存在一个父类,要实现一个getSum方法,用来过滤出大多数的子类,然后将剩余对象的某个字段相加并且返回其总和。
那么除了将所有子类都load进内存(在父类的一个Collection中)再继续过滤,我想会有更好的方法来优化。我在DAO建立了一个方法用来发送Query给Database,进行过滤和计算总和。
出于分层的目的,我不想让外部调用的时候直接访问DAO,而应该在其他层来调用getsum。我的问题就是到底应该在那层来调用getsum方法?到底是在entity Domain Object中还是在Service中?

 1class MyParentEntity
 2{
 3  private MyDao dao;
 4  
 5  public void setDao(MyDao dao)
 6  {
 7    this.dao = dao;
 8  }

 9
10  public int getSum(FilteringParams params)
11  {
12    return dao.getSum(params, this);
13  }
   
14
15  // other operations
16}
或者
 1class MyServiceImpl implements MyService
 2{
 3  private MyDao dao;
 4  
 5  public void setDao(MyDao dao)
 6  {
 7    this.dao = dao;
 8  }

 9
10  public int getSum(FilteringParams params, MyParentEntity parentEntity)
11  {
12    return dao.getSum(params, parentEntity);
13  }
   
14
15  // other operations
16}
在第一个例子中,客户端将直接调用MyParentEntity.getSum().在第二个例子中客户端将使用MyService.getSum()来取代,使用MyParentEntity一个实例来作为参数。
把getSum放入MyParentEntity看起来回比较合适,毕竟它包含了一个MyParentEntity的Instance,但另一方面来看它同时也调用了DAO中的方法


-----------------reply--------------------
基本的问题是你是否认为将Domain object Loader(你的Dao)作为domain object的一部分,但我并不是这么想的。
DAO本身也是一个Service,因此我想它不应当存在在Domain object.
Domain Object 并不是贫血的,因为它需要collaborators,它通过在Runtime时Collaborator.
因此你应该这样做:
public int getSum(ChildEntityLocatorStrategy, FilteringParams)
ChildEntityLocatorStrategy 是一个用来搜索相关的entities的接口。当然DAOChildEntityLocatorStrategy会简单的调用dao.getSum(params, parent).
----------------------reply-----------
谢谢你的评论,刚刚修改如下:
 1class MyParentEntity
 2{
 3  public int getSum(ChildEntityLocatorStrategy childLocator, FilteringParams params)
 4  {
 5    childLocator.getSum(params, this);
 6  }

 7
 8  // other operations
 9}

10
11interface ChildEntityLocatorStrategy
12{
13  public int getSum(FilteringParams params, MyParentEntity parentEntity);
14}

15
16class DAOChildEntityLocatorStrategy implements ChildEntityLocatorStrategy
17{
18  private MyDao dao;
19 
20  public void setDao(MyDao dao)
21  {
22    this.dao = dao;
23  }

24
25  public int getSum(FilteringParams params, MyParentEntity parentEntity)
26  {
27    return dao.getSum(params, parentEntity);
28  }
   
29
30  // other operations
31}
--------reply----------------
http://thread.gmane.org/gmane.comp.java.springframework.user/3402

http://thread.gmane.org/gmane.comp.java.springframework.user/3513

----------reply---------------
http://www.digizenstudio.com/blog/2005/06/07/inject-into-domain-objects/
posted on 2005-11-01 09:53 老妖 阅读(591) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

随笔分类(48)

随笔档案(104)

好友链接

我的豆瓣

积分与排名

  • 积分 - 218839
  • 排名 - 257

最新评论

阅读排行榜