在entity Domain Object 中存在一個父類,要實現一個getSum方法,用來過濾出大多數的子類,然后將剩余對象的某個字段相加并且返回其總和。
那么除了將所有子類都load進內存(在父類的一個Collection中)再繼續過濾,我想會有更好的方法來優化。我在DAO建立了一個方法用來發送Query給Database,進行過濾和計算總和。
出于分層的目的,我不想讓外部調用的時候直接訪問DAO,而應該在其他層來調用getsum。我的問題就是到底應該在那層來調用getsum方法?到底是在entity Domain Object中還是在Service中?
1
class 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
} 或者
1
class 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-----------
謝謝你的評論,剛剛修改如下:
1
class MyParentEntity
2

{
3
public int getSum(ChildEntityLocatorStrategy childLocator, FilteringParams params)
4
{
5
childLocator.getSum(params, this);
6
}
7
8
// other operations
9
}
10
11
interface ChildEntityLocatorStrategy
12

{
13
public int getSum(FilteringParams params, MyParentEntity parentEntity);
14
}
15
16
class 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/3402http://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
老妖 閱讀(605)
評論(0) 編輯 收藏