如何(或应该)避免在jsf中使用长方法/类 [英] How to (or should you) avoid long methods/classes in jsf

查看:85
本文介绍了如何(或应该)避免在jsf中使用长方法/类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这一直困扰着我,但是当我寻找可以做的事情时,大多数时候我所能做的就是将一个长方法划分为n个小方法.这使您的课程仍然很长,有时也很难阅读.

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?

我认为这与jsf没有直接关系,但与您要使用控制器备份"视图的任何模型都没有直接关系.因此,一般性建议也将有所帮助.

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.

推荐答案

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

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.

例如因此不是

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方法委托的,就像这样

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...

这完全没有必要.

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

实体本身也可以进一步划分.例如. UserstreethouseNumberzipCodecitycountry可以由同一User中的另一个实体/DTO Address代替.

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.

如果运气不好,则代码是由可视化编辑器(例如Netbeans + Woodstock)自动生成的.无论如何,在没有完全重新设计的情况下没有太多重构的余地,我宁愿寻找另一个项目.

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.

这篇关于如何(或应该)避免在jsf中使用长方法/类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆