我突然想到将我的服务器端任务编写为一个大服务,并将所有 dao 对象注入(inject)该服务中 - 如下所示:
public class MyServiceImpl extends RemoteServiceServlet implements MyService {
@Inject
MyDAO1 myDAO1;
@Inject
MyDAO2 myDAO2;
@Inject
MyDAO3 myDAO3;
...
//20 methods
//
}
是否需要一项大型服务,或者是否有其他更好的模式?
最佳答案
您应该为每个任务“域”构建一个服务。例如,用户管理服务,其方法允许注册新用户、发送恢复密码电子邮件、暂时禁用用户、获取当前启用的用户列表等。
我建议您看一下 Martin Fowler 的企业应用程序架构模式
( http://martinfowler.com/books/eaa.html ) 中描述的 ServiceLayer 模式。
A Service Layer defines an application's boundary [Cockburn PloP] and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coor-dinating responses in the implementation of its operations (http://martinfowler.com/eaaCatalog/serviceLayer.html)
关于java - 使用多个注入(inject) DAO 时的最佳实践是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743511/