【发布时间】:2017-07-21 16:58:30
【问题描述】:
我正在使用 javaSE 开发小应用程序,只是为了提高我的技能。所以我有业务服务(进一步的BS),其中一些方法如registerUser(User user),addAmount(Long accountId)。 BS 使用 dao。让我们假设 BS 是从 WS 或其他接口元素调用的。我有以下 DAO:
public interface UserDao {
User getUserByUsername(String username);
void saveUser(User user);
}
public interface AccountDao {
void saveAccount(Account account);
Account getAccountByUserId(Long userId);
}
我的 BS 看起来像
public class FastAccountServiceImpl implements FastAccountService{
private UserDao userDao;
private AccountDao accountDao;
public void registerUser(User user, Account account) throws Exception {
if (user.getUsername() == null || user.getUsername().isEmpty()) {
throw new Exception("all params are mandatory");
}
if (userDao.getUserByUsername(user.getUsername()) != null) {
throw new Exception("This user exists");
}
userDao.saveUser(user);
accountDao.saveAccount(account);
}
public void withdrawAmount(Double amount, Long userId) throws Exception {
if (amount == null || userId == null) {
throw new Exception("amount and userId are mandatory");
}
Account account = accountDao.getAccountByUserId(userId);
if (account.getAmount() == null || account.getAmount().compareTo(amount) < 1) {
throw new Exception("Insufficient amount in account");
}......
}}
所以我的第一个问题是我应该在哪里检查参数是否为 null 等?在BS? 第二个问题是为什么我们应该为每张桌子设置单独的 dao ?我可以为所有表只创建一个 dao。所以在 BS 中只有一个 dao。该 dao 表示如下:
public interface MagicDao {
User getUserByUsername(String username);
void saveUser(User user);
void saveAccount(Account account);
Account getAccountByUserId(Long userId);
}
【问题讨论】:
-
这对于 StackExchang'es Software Engineering 网站来说可能是一个更好的问题,但请务必阅读他们的规则和帮助中心,以确保您遵循他们的指导方针。
-
@DanielBickler 在引用其他网站时,指出cross-posting is frowned upon 通常会有所帮助
-
@gnat 这很有意义......我认为这个问题可能会因为偏离主题或基于意见而被关闭,因此它不会被交叉发布。这不正确吗?