Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供回调接口或组件,用于在具体的填充过程支持一些自定的操作 #79

Closed
Createsequence opened this issue May 30, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request worth trying Features or improvements that are worth trying but not necessarily achievable
Milestone

Comments

@Createsequence
Copy link
Collaborator

Createsequence commented May 30, 2023

有时候,当我们配置好操作时,比如:

@Assemble(container = "user", props = @Mapping("gender"))
private Integer id;
private String gender;

实际上这个 gender 很有可能并不是直接把根据 id 关联出来的对象的 gender 属性搬过来就完了,有可能还会有一些别的操作,比如:

  • 属性转换:比如数据源对象的属性是 List<Integer>,我只想取首个元素再转字符串,这显然不能指望靠类型转换器解决;
  • 追加操作:比如,我获取到的可能是“男”或者“女”,而我需要转为“先生”或者“小姐”

上述两个例子可能不够恰当,严格来说它们都算追加操作,但是要表达是意思是一样的:“当获取到了关联的对象后,我希望能够做些除了属性映射以外的事情”,#5 想要的效果其实就是这个。

目前比较好的思路是给个 AssembleOperation 以及 DisassembleOperatiopn 加一个 interceptors,它可能是绑定在:

  • 装配/拆卸操作上:那么它应该能感知到 targetsource 对象;
  • 字段映射配置上:那么它应该能感知到 targetsource 对象,以及两者要映射的属性值;

比较迷茫的点是:

  • 如果是绑定到操作上,那它跟现有的 OperationHandler 功能是不是有部分重叠?
  • 如果要绑定到字段映射配置上,那它的功能和 PropertyOperator 的功能是不是有部分重叠?

这个功能肯定是要的,因为公司现在就遇到了这样的需求,但是到底要怎么样比较好的基于现有代码实现,这就是个问题了。

@Createsequence Createsequence added the enhancement New feature or request label May 30, 2023
@Createsequence Createsequence self-assigned this May 30, 2023
@Createsequence Createsequence added this to the release 1.4.0 milestone Jun 9, 2023
@Createsequence Createsequence added the worth trying Features or improvements that are worth trying but not necessarily achievable label Jul 6, 2023
@Createsequence
Copy link
Collaborator Author

Createsequence commented Jul 10, 2023

2023-07-11 补充:
#100 似乎也可以实现类似的效果,本 issues 可以再观望一阵,确认是否真的需要这样的功能。

@Createsequence Createsequence removed their assignment Jul 19, 2023
@Createsequence Createsequence changed the title 提供操作拦截器,用于在具体的填充过程支持一些自定的操作 提供回调接口或组件,用于在具体的填充过程支持一些自定的操作 Jan 20, 2024
@Createsequence Createsequence self-assigned this Jan 20, 2024
@Createsequence
Copy link
Collaborator Author

目前想到了一个比较好的办法,那就是像 Spring 一个提供一个 OperationAware 接口,用户可以让自己被填充的 Java 类实现这个接口,然后执行器在执行的时候,就根据不同的阶段进行触发这个回调方法。

比如:

@Accessors(chain = true)
@Data
private static class Bean implements SmartOperationAware {
    @Assemble(groups = {"op", "id"})
    private Integer id;
    private String name;

    @Assemble(groups = {"op", "key"})
    private Integer key;
    private String value;

    @Disassemble(type = NestedBean.class, groups = "op")
    private NestedBean nestedBean;

    @Override
    public boolean supportOperation(KeyTriggerOperation operation) {
        // 执行装配或拆卸操作前,检查是否要真正的应用这个操作
        return true;
    }

    @Override
    public void beforeAssembleOperation() {
		// 在拆卸操作完成后,装配操作开始前执行
    }
    @Override
    public void beforeAssembleOperation(BeanOperations operations) {
		// 在拆卸操作完成后,装配操作开始前执行
    }
    
    @Override
    public void afterOperationsCompletion(BeanOperations operations) {
		// 在所有的操作完成后执行
    }
    @Override
    public void afterOperationsCompletion() {
		// 在所有的操作完成后执行
    }
}

在操作执行器执行的过程中,判断一下这个对象是不是实现了 OperationAware 接口,即可根据情况调用相应的方法。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request worth trying Features or improvements that are worth trying but not necessarily achievable
Projects
None yet
Development

No branches or pull requests

1 participant