gpt4 book ai didi

java - 在 JAVA 和 Spring 中设计自定义工作流程

转载 作者:行者123 更新时间:2023-11-30 06:10:39 24 4
gpt4 key购买 nike

我正在开发 spring 2.0.1.RELEASE 应用程序。

Brief of Application:

1. I have separate Transformer beans that transforms my DTO to Domain
and vice versa.
2. I have separate Validator beans that validate my domain object being passed.
3. I have Service classes that takes care of the applying rules and calling persistence layer.
<小时/>

Now, i want to build a Workflow in my application: where i will just call the start of the workflow and below mentioned steps will be executed in order and exception handling will be done as per the step:

1.First-Transformtion - transformToDomain() method will be called for that object type.  
2.Second-Validator - class valid() method will be called for that object.
3.Third-Service - class save() method will be called for that object.
4.Fourth- Transformation - transformToDTO() method will be called for that object type.

在此之后,我的工作流程结束,我将返回 DTO 对象作为 REST API 的响应。

异常处理部分是我也想处理的部分,比如如果该步骤存在特定的异常处理程序,则调用它,否则调用全局异常处理程序。

我设计了一些相同的原型(prototype),但正在寻求一些专家建议以及如何通过更好的 java 设计来实现这一点。

<小时/>

考虑上述用例的示例解释非常值得赞赏。

最佳答案

我不太确定你所描述的是否是真正意义上的工作流程系统,也许责任链更符合你所说的?

按照您所描述的执行顺序,下面是我如何实现该链的简化示例:

变压器.java

public interface Transformer<IN, OUT> {

OUT transformToDomain(IN dto);

IN transformToDTO(OUT domainObject);
}

validator .java

public interface Validator<T> {

boolean isValid(T object);
}

服务.java

public interface Service {

void save(Object object);
}

以及绑定(bind)所有内容的实现:ProcessChain.java

public class ProcessChain {

private Transformer transformer;
private Service service;
private Validator validator;

Object process(Object dto) throws MyValidationException {
Object domainObject = transformer.transformToDomain(dto);
boolean isValid = validator.isValid(domainObject);
if(!isValid){
throw new MyValidationException("Validation message here");
}
service.save(domainObject);
return transformer.transformToDTO(domainObject);
}
}

我在这里没有指定任何与 Spring 相关的内容,因为你的问题似乎是一个设计问题而不是技术问题。

希望这有帮助

关于java - 在 JAVA 和 Spring 中设计自定义工作流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50310832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com