如何(或者应该)避免 jsf 中的长方法/类

发布于 2024-11-16 01:41:53 字数 255 浏览 4 评论 0原文

我主要在基于 JSF 的项目中处理遗留代码,并且支持 bean 中有很多相当长的类和方法。

这一直困扰着我,但是当我寻找可以做什么时,大多数时候我能想到的就是将一个长方法分成 n 个小方法。这会给你带来很长的课程,有时也更难阅读。

那么,如何才能让你的支持 bean 变得简短明了呢?或者你会在一页上保留一个大的支撑豆吗?有没有最佳实践?

我认为这与 jsf 没有直接关系,而是与您使用控制器“备份”视图的任何模型相关。因此,一般建议也会有所帮助。

I'm mostly working with legacy code in a JSF based project and there are lots of quite long classes and methods in backing beans.

This is constantly bugging me but when I look for what can be done, most of the time all I can come up is to divide a long method to n small methods. Which gives you still a very long class and sometimes harder to read too.

So what do you do to keep your backing beans short and concise? Or do you keep one big backing bean for one page? Are there any best-practices?

I assume this is not directly related to jsf but to any model where you are 'backing' up your view with a controller. So a general advise would be helpful also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽心 2024-11-23 01:41:53

将所有字段放在自己的类中,也称为实体或 DTO 类(例如 UserProductOrder 等)并引用它。这些可以是 JDBC/JPA 实体。将所有业务方法放在自己的类中,也称为服务或域对象(例如UserServiceProductService等)并引用它。这些可以是 EJB 的。

例如,因此不是

public class Bean {

    private Long id;
    private String field1;
    private String field2;
    private String field3;
    // Etc... All model fields.

    @PostConstruct
    public void init() {
        // >20 lines of business/domain code to prefill the fields from DB.
    }

    public void save() {
        // >20 lines of business/domain code to save the fields in DB.
    }

    // Getters/setters for all fields and possibly also all nested fields.
}

但更进一步

public class Bean {

    private Long id;
    private Entity entity;

    @EJB
    private EntityService service;

    @PostConstruct
    public void init() {
        entity = service.find(id);
    }

    public void save() {
        service.save(entity);
    }

    // Getter/setter for id and getter for entity.
}

,我还看到了代码,其中嵌套对象/实体是通过 bean 中的附加 getter/setter 委托的,就像这样

private Entity entity;

public String getField1() {
    return entity.getField1();
}

public void setField1(String field1) {
    entity.setField1(field1);
}

// Etc...

这是完全没有必要的。实体只需一个 getter 就足够了(setter 不是强制的!),结合

<h:inputText value="#{bean.entity.field1}" />

实体本身还可以进一步划分。例如 streethouseNumberzipCodecitycountry User 可以被同一 User 内的另一个实体/DTO Address 替换。


如果您运气不好,代码已由可视化编辑器(例如 Netbeans + Woodstock)自动生成。无论如何,如果不完全重新设计它,就没有太多需要重构的地方,我宁愿寻找另一个项目。

Put all fields in its own class, also called an entity or DTO class (e.g. User, Product, Order, etc) and reference it instead. Those can be JDBC/JPA entities. Put all business methods in its own class, also called a service or domain object (e.g. UserService, ProductService, etc) and reference it instead. Those can be EJB's.

E.g. thus not

public class Bean {

    private Long id;
    private String field1;
    private String field2;
    private String field3;
    // Etc... All model fields.

    @PostConstruct
    public void init() {
        // >20 lines of business/domain code to prefill the fields from DB.
    }

    public void save() {
        // >20 lines of business/domain code to save the fields in DB.
    }

    // Getters/setters for all fields and possibly also all nested fields.
}

But more so

public class Bean {

    private Long id;
    private Entity entity;

    @EJB
    private EntityService service;

    @PostConstruct
    public void init() {
        entity = service.find(id);
    }

    public void save() {
        service.save(entity);
    }

    // Getter/setter for id and getter for entity.
}

Further, I've also seen code wherein the nested objects/entities are delegated through by additional getters/setters in the bean like so

private Entity entity;

public String getField1() {
    return entity.getField1();
}

public void setField1(String field1) {
    entity.setField1(field1);
}

// Etc...

This is totally unnecessary. Just a single getter for the entity is sufficient (setter is not mandatory!), in combination with

<h:inputText value="#{bean.entity.field1}" />

The entities at its own can also be further divided. E.g. street, houseNumber, zipCode, city, country of an User could be replaced by another entity/DTO Address within the same User.


If you have bad luck, the code has been autogenerated by a visual editor (e.g. Netbeans + Woodstock). There's not much to refactor anyway without completely redesigning it, I would rather look for another project.

我乃一代侩神 2024-11-23 01:41:53

恕我直言,每个方法应该是 1 个步骤,如果该 1 个步骤包含多个内部步骤,您应该将它们分解为更小的方法,以便更容易阅读。现代 IDE 的好处是它们可以毫不费力地为您折射代码。

IMHO, each method should be 1 step, if that 1 step conatins multiple inner steps you should break them down into smaller methods so its easier to read. The good thing with modern IDE's is that they refractor code for you without much effort at all.

触ぅ动初心 2024-11-23 01:41:53

不仅适用于支持 bean,而且适用于一般的所有对象:

您应该始终通过以下方式重构 长方法 将它们提取成多个较小的。

Not only applicable to backing beans, but to all objects in general:

you should always refactor long methods by extracting them into multiple smaller ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文