Spring控制器和服务设计
我在数据库中存储了不同类型的任务。我需要从数据库获取任务并在适当的服务中运行每个任务(例如,AService 处理 ATask 对象,BService 处理 BTask 对象等)。还有一个 TaskController 类,用于协调任务的执行。
哪个设计更好 -
1.让控制器获取所有活动任务并将每个任务发送到适当的服务。
2.让每个服务获取并执行与其相关的所有任务(在这种情况下,控制器必须循环所有定义的服务并激活它们)。
I have tasks of different types stored in the DB. I need to fetch the tasks from DB and run each task in its appropriate service (e.g. AService handles ATask objects, BService handles BTask objects etc.). There is also a TaskController class which coordinates the execution of tasks.
Which is the better design -
1.Have the controller fetch all active tasks and send each task to the appropriate service.
2.Have each service fetch and execute all the tasks that are relevant to it (in this case the controller would have to loop over all the defined services and activate them).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会采用解决方案 1,但应创建一个中间服务来处理您想要放入控制器中的内容 - 加载和分派任务。
我还闻到了巨大的
switch
或一连串if
的味道,也许还有instanceof
。我想您可以从责任链模式中受益- 询问第一个服务是否可以处理该任务。如果不能,则传递给第二个,依此类推。另一种方法是访问者,如果每个任务都是一个单独的子类普通类。
I would go for solution 1. with the exception that an intermediary service should be created to handle what you want to put in the controller - loading and dispatching tasks.
Also I smell big fat
switch
or a cascade ofif
s, maybe withinstanceof
. I guess you can benefit from chain of responsibility pattern - ask first service whether it can handle the task. If it can't - pass to the second one and so on.Another approach would be a visitor if each task is a separate subclass of common class.