我正在爲網站編寫功能區/成就係統,我必須爲系統中的每個功能區編寫一些邏輯。例如,如果您在前2000人註冊到網站或在論壇中發佈了1000個帖子後,就可以獲得功能區。這個想法非常類似於stackoverflow的徽章。將DAO自動裝入域對象
因此,每個功能區顯然都在數據庫中,但他們也需要一些邏輯來確定用戶何時贏得功能區。
在我編寫的方式,Ribbon
是一個簡單的抽象類:
@Entity
@Table(name = "ribbon")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ribbon_type")
public abstract class Ribbon
{
@Id
@Column(name = "id", nullable = false, length = 8)
private int id;
@Column(name = "title", nullable = false, length = 64)
private String title;
public Ribbon()
{
}
public abstract boolean isEarned(User user);
// ... getters/setters...
}
你可以看到我定義繼承策略爲SINGLE_TABLE
(因爲我有如下代碼50個絲帶,我不需要其中任何一個的附加列)。
現在,一個特定的帶將被這樣實現,例如:
@Entity
public class First2000UsersRibbon extends Ribbon
{
@Autowired
@Transient
private UserHasRibbonDao userHasRibbonDao;
public First2000UsersRibbon()
{
super.setId(1);
super.setTitle("Between the first 2,000 users who registered to the website");
}
@Override
public boolean isEarned(User user)
{
if(!userHasRibbonDao.userHasRibbon(user, this))
{
// TODO
// All the logic to determine whether the user earned the ribbon
// i.e. check whether the user is between the first 2000 users who registered to the website
// Other autowired DAOs are needed
}
else
{
return true;
}
return false;
}
}
的問題是userHasRibbonDao
是isEarned()
方法內空,所以NullPointerException
被拋出。
我認爲DAO自動裝入域對象是錯誤的,但在this topic他們告訴我,這是正確的方法(域驅動設計)。
我共享的非工作GitHub上很簡單的例子:https://github.com/MintTwist/TestApp(記得要改變連接細節/WEB-INF/properties/jdbc.properties並導入test_app.sql腳本)
任何幫助非常感激。
謝謝!
更新 - 閱讀第一個答案,似乎我的方法是完全錯誤的。考慮到可能存在50-70個不同的色帶,您如何理想地構建代碼?由於
> _
這裏應該有一些github網址嗎? – madth3
謝謝@NathanHughes,我剛剛發佈了他們在另一個問題上告訴我的內容。 – satoshi